- How do I create a copy of a directory in Unix/Linux? [closed]
- 3 Answers 3
- How To Go Back to a Directory in Linux
- The Linux Directory Structure (For New Linux Users)
- How To Go Back to a Directory in Linux
- How To Move to the Parent Directory of the Current Parent Directory (Two Levels Up)
- Using the TAB Key With the cd Command to Autocomplete Paths
- How To Move to the Home Directory in the Linux Terminal
- Move to a Directory With Spaces in its Name
- Conclusion
- About the author
- David Adams
- Automatic backup of folder
- 3 Answers 3
How do I create a copy of a directory in Unix/Linux? [closed]
I want to recursively create a copy of a directory and all its contents (e.g. files and subdirectories).
3 Answers 3
The option you’re looking for is -R .
cp -R path_to_source path_to_destination/
- If destination doesn’t exist, it will be created.
- -R means copy directories recursively . You can also use -r since it’s case-insensitive.
- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use -a flag along with trailing /. in the source (as per @muni764 ‘s / @Anton Krug ‘s comment):
cp -a path_to_source/. path_to_destination/
i wonder why this exact command in dockerfile copies all source directory files into destination, instead of copying just whole directory.
I believe the ‘/’ on the end makes a difference and that might account for your experience. If the source includes the trailing slash it will copy what is in the directory only. If it does not include the trailing slash, it will copy the directory as well and then the contents inside of it. My memory is this behavior varies by command and maybe event by OS a bit. Here’s a reference with more info.
I would say if you don’t want to include the source and you want to make sure everything is copied (symlinks, hidden files) without copying the source parent folder is to use -ra source/. destination. This will make sure the content of the folder is copied, but not the parent folder itself, which is sometimes handy. And the difference is the /.
Note the importance of «Slash dot» on your source in cp -r src/. dest I know it is mentioned but I still seem to miss it every time.
You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.
If the directory you’re copying is called dir1 and you want to copy it to your /home/Pictures folder:
Linux is case-sensitive and also needs the / after each directory to know that it isn’t a file. ~ is a special character in the terminal that automatically evaluates to the current user’s home directory. If you need to know what directory you are in, use the command pwd .
When you don’t know how to use a Linux command, there is a manual page that you can refer to by typing:
Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you’ve started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.
There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox) -R , -r , and —recursive are all equivalent, as mentioned in this answer. For portability, i.e. POSIX compliance, you would want to use -R because of some implementation-dependent differences with -r . It’s important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).
How To Go Back to a Directory in Linux
This tutorial shows how to go back to the previous or parent directories using the cd (Change Directory) command. Additionally, this content includes a complete guide to move quickly between directories in the Linux terminal.
This tutorial is optimized for inexperienced Linux users. By reading this documentation, they will fully understand how the cd (Change Directory) command works and its applications. All instructions described include screenshots, making it easy for every Linux user to understand and apply them.
The cd command implementations to move between directories in the Linux terminal explained in this article include the following cd usage examples:
The cd Command Options | Function |
cd – | Move to the previous directory |
cd .. | Move to the parent directory |
cd ../.. | Move to the parent directory of the parent directory (Two levels up) |
cd ../../.. | Move three levels up |
cd | Autocomplete path or show available subdirectories |
cd | Move to the home directory |
cd ~ | Move to the home directory |
cd ~ | Move to home directory |
cd ‘Directory named with spaces’ | Use quotation marks to move to a directory with spaces in name |
The Linux Directory Structure (For New Linux Users)
Note: If you already know the basic Linux directory structure, you can jump straight to instructions to go back to previous or parent directories.
First, I want to remind new Linux users that the Linux directory structure is hierarchical. Directories within directories are subdirectories. The directories and subdirectories tree are what we call the “path”. When we specify a path, we are specifying parent directories and subdirectories.
In Linux, the main directory is what we call the root directory. It is the top directory containing the rest of the system directories. Users see a slash after their hostname when the current directory is the root directory, as shown in the following image (Violet slash).
The root directory contains system core directories, including the /boot directory, which contains boot partition and/or directories and files, and the /usr partition and/or directory, which contains program directories and files.
The following image shows the / (root) directory content, most of which is universal for all Linux distributions:
For example, in the path /etc/apt/, the root directory / (First slash) is the parent directory of the /etc subdirectory, which is the parent directory of the /apt directory.
How To Go Back to a Directory in Linux
This section explains how to go back to your previous current directory and to go back to a parent directory using the cd command in Linux.
In the following example, the linuxhint user’s first current directory is his home directory. In the first line, the user uses the cd command to move to the /etc directory, following the hierarchical path beginning from the root directory.
To go back from the current directory (/etc/) to the previous one, the user runs the cd command followed by a hyphen, as shown in the second line of the following figure:
As you can see in the previous image, the user returned to the previous directory by running cd -.
The previous example explains how to return to the previous directory the user was in.
Now, let’s say the user does not want to return to the previous directory he was in, but he wants to go back to the hierarchical tree.
Here, a user is in his home directory, and he moves to the /etc/apt subdirectory. Instead of returning to the previous directory he was in (His home directory), he wants to move to the parent directory of the current one (In this case, the parent directory of /apt is /etc). For this, the user will run the command cd followed by two dots, as shown below:
As you can see in the previous figure, the user moved to the parent directory of the previous one (/etc) and not to the previous directory he was in (/home/linuxhint).
As you can see, cd – moves the user to his previous current directory, while cd .. moves him to the current parent directory.
How To Move to the Parent Directory of the Current Parent Directory (Two Levels Up)
As said in the previous example, the cd .. command allows us to move to the parent directory. If the user wants to move to the parent directory of the parent directory (Two levels up), he needs to type the two dots twice, separated by a slash.
In the following example, the user’s current directory is linuxhint2, located under the linuxhint directory, which is located under the Desktop directory within the user home.
Let’s say the user wants to move two levels up, from the linuxhint2 directory to the Desktop directory. In this case, he only needs to use the two dots twice, separated by a slash, as shown in the following screenshot:
The previously executed command instructs Bash to take the user two directories up in the hierarchical tree. You also would implement additional dots and slashes to move to upper levels in the directory tree.
Using the TAB Key With the cd Command to Autocomplete Paths
The keyboard key tab plays an important role when using the cd command. It helps users move between directories without typing the whole path and without knowing the final path in advance.
In the following example, four subdirectories begin with the same name, but only the last parts of their names change: such as linuxhint, linuxhint2, and linuxhint3.
Suppose the user runs cd linuxhint and presses the TAB key twice. Then, the console returns all existing subdirectories, whose names begin with the path typed by the user. Thus, informing the user of the available subdirectories for him to type the complete full path.
In the following image, the user executed cd Desktop/linuxhint and then pressed the TAB key:
Even if the user does partially type the destination full path, the TAB key will also show all available subdirectories within the directory we want to access.
In the following example, the user knows he wants to access a subdirectory within his home directory. But he does not know what is the specific subdirectory he wants to access.
In this case, the user can run cd /parentdirectory/ and press the TAB key twice. This will print all subdirectories within the parent directory:
How To Move to the Home Directory in the Linux Terminal
Moving to your home directory is pretty simple. Just run the cd command without additional options, as shown below:
You also can use a tilde (~) to specify your home directory as a path. By running the cd command followed by a tilde, you will move to your home directory.
You can use the tilde to move to any user’s home directory. To accomplish it, type the username whose directory you want to move to, after the tilde symbol. As shown in the following example, the linuxhint user moves to the torvalds directory.
Move to a Directory With Spaces in its Name
Let’s assume the user wants to move to a directory whose name contains spaces to end this tutorial. In this case, the user only needs to type the directory name between quotation marks, as shown in the following screenshot.
In the following example, the user implements quotation marks to move into the Linux Hint directory:
Conclusion
The article discussed how the cd command allows users to move between different locations faster than the graphical user interface. All commands shown in this content are easy to learn and implement, and required for any Linux user.
The cd command is also available in other operating systems, like macOS or MSDOS, and the command is universal for all Linux distributions. I recommend readers to utilize the provided examples to make terminal browsing easier.
I hope all tips provided are useful for new users. Keep following us for more professional Linux articles.
About the author
David Adams
David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.
Automatic backup of folder
I have one particular folder on my linux box that I would like to have fully backed up and be able to «go back in time» on a file and see all previous revisions. To add complications, this folder is shared (with the inbuilt sharing tools) and is accessed and written to by a Windows machine. For this reason, I’d like the backup to be written to a place where the windows machine does not have access. How can I set this up?
3 Answers 3
Well I use the following script for my backup:
#! /bin/bash # Gets date of most recent backup. newestfile=$(cd /home//.Backups && find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ") budate=`echo $newestfile| cut -c10-19` # Gets current date cdate=$(date --iso) # If the cureent date is the same as the date of the most recent backup, don't run the backup, just give a notification that says it has already been done today. if [ $cdate = $budate ]; then echo "Backup Complete" notify-send -i /home//Pictures/Logos/safe.png "Backup Status" "Already started/finished backup for today." # If the dates are different, start the backup. else echo "Starting backup" notify-send -i /home//Pictures/Logos/safe.png "Backup Status" "Starting backup for today." # Compresses the files into .tar.gz format tar -cvpzf /home//.Backups/backup-$(date +%Y-%m-%d-%H:%M).tar.gz "/home//folder/to/back/up" --exclude=.Backups && notify-send --expire-time=60000 -i /home/tim/Pictures/Home/Logos/safe.png 'Backup Status' 'Finished backup for today.' fi
This will save a backup file that looks like this:
In the hidden folder /home//.Backups
The safe.png file that it used for the notifications can be downloaded from here.
- Save the script in /home//Scripts as backup.sh
- Run the following commands:
chmod +x Scripts/backup.sh mkdir .Backups touch .Backups/backup-2000-01-01-00:00.tar.gz
0 15 * * * bash /path/to/script/backup.sh