This article is designed for all users who are learning Linux. We will get acquainted with the commands that allow you to copy, move, create, delete files and directories.
We will cover the commands e.g touch , rm , cp , mv , mkdir , rmdir .
touch - create a file
rm - delete the file
cp - used to copy file or folder
mv - used to move file or folder
mkdir - create a folder
rmdir - delete folder
touch – utility for setting the time of access/modification of files. The main purpose of the utility is, as you understand, set the date and time the file was last modified or accessed.
However, this utility is very often used when creating files. The point is that if the utility passes the name of a file that does not exist in the argument, the program will automatically create it and set the time of the change and access.
-t – with this key we can set the time in the format that corresponds to the pattern [CCYYMMDDhhmm.SS] .
- CC – the first 2 digits of the year (century)
- YY – last 2 digits of the year
- MM – month
- DD – day
- hh – hours
- mm – minutes
- SS – seconds
Example: touch -t 201202270446.26 file.txt
For the file file.txt, the date will be set on February 27, 2012 at 4:46:26.
There are other keys for this utility, we will not disassemble them for now since most often this utility is used to create empty files.
rm is a utility for deleting files and directories. The main purpose is to delete files, but with the -R option, you can also delete the directory. This key is used to delete a directory in which there are already files, you can use the rmdir command to delete empty folders.
<strong>-f</strong> - ignores all errors and performs deletion, without giving out any information to the console.
<strong>-i</strong> - Displays a warning when the file is deleted. You must confirm or cancel the deletion.
<strong>-v</strong> - displays information about the performed actions.
The command as an argument (the name of the file to be deleted) can accept special characters (group operations), we’ll talk about this later, and now just look at a couple of examples.
<strong>rm *.txt</strong> - delete all files with the <strong>txt</strong> extension in the directory
<strong>rm foo*</strong> - deletes all files whose name starts with <strong>foo</strong>
cp – utility for copying files and directories. As arguments, it takes keys (options), source path, destination path.
cp [OPTIONS] SOURCE DESTINATION
cp -v /var/log/messages/home/user/messages
The command itself is very flexible, has a lot of keys, but in practice, they are not used often. The keys -f, -v, -i, -r (-R) are most often used. Functions of which are similar to the command rm.
If there is already a file with this name in the directory where the file is to be copied, it will be overwritten. To avoid such situations, it is desirable to use the -i switch. Just like the rm command, cp can accept special characters to copy multiple files and directories at once.
This utility is used to move and rename files. It has the same syntax as cp, and the keys perform functions like cp.
Accordingly, we get the syntax of the command:
mv [OPTIONS] SOURCE DESTINATION
mv -v /var/log/messages/home/user/messages
Thus, the source file will be created at the destination with a new name. You can use special characters to move or rename multiple files at once.
The mkdir utility is used to create empty directories. You can also create subdirectories, sometimes quite conveniently. The syntax is quite simple: mkdir [OPTION] folder . As options (keys) takes only 3 keys:
-m - sets permissions for the directory. You must specify the rights using the umask mask.
-p - allows you to create subdirectories.
-v - displays information about the performed actions.
Using the -p switch is quite simple, we just need to specify the name of the sub-directories via the slash, so the syntax will look like this:
mkdir -p test/test1/test2/test3
After executing the command, a test directory will be created in which the sub-directory test1 will be created and so on.
The utility for removing directories. Removes only empty directories. Also has in the arsenal the -p switch, to delete sub-directories. Sub-directories should be empty accordingly.
Practice working with these commands to consolidate the material. Since these commands you will use most often, you need to bring the use of these commands to automatism.
Join our reader's list to start receiving latest updates.
Get free Tech, Linux tips and resources delivered directly to your inbox.