In the previous tutorial you saw some basic linux commands, now you will see some of the commands that are used to create and manipulate files.
Touch
The touch command is used to create a file. Just type touch followed by the filename and your file will be created!
1 |
touch filename.txt |
If a file with the same filename already exists then it will update the timestamp(created/modification date) of the file.
Mkdir(make directory)
mkdir is used to create a new directory.
1 |
mkdir MyDirectory |
Rmdir(remove directory)
rmdir is used to delete an exiting directory. Remember! rmdir will only removes those directories that are empty. You will see how to delete non-empty directories with the upcoming command rm.
1 |
rmdir MyDirectory |
RM(remove)
rm is a very powerful command, you can delete almost anything with it, so be very careful while using it. Files/Directories deleted with rm do not go to bin, once deleted they are gone and cannot be recovered.
To delete a file, write rm followed by the filename.
1 |
rm filename.txt |
-r
To delete a directory you have to use the -r option. It means that you want to delete all the files recursively.
1 |
rm -r MyDirectory |
-i
When you use -i option, before deleting any file it will ask you for a confirmation whether you want to delete it or not. It is useful in the case where you want to delete some files in a directory while keeping the others.
1 |
rm -i -r MyDirectory |
Output
1 2 3 4 |
examine files in directory MyDirectory? y remove MyDirectory/file1? n remove MyDirectory/text.tst? y remove MyDirectory? n |
-f
The f here means force delete, it means what ever be the case don’t ask me for any confirmation, just delete it!
1 |
rm -f -r MyDirectory |
-v
The v here means verbose, if you add this option then the rm command will print a summary of all the files/directories that it has deleted.
1 |
rm -v -r MyDirectory |
Output
1 2 3 |
MyDirectory/song.mp3 MyDirectory/test.txt MyDirectory |
CP(copy)
To copy a file from to a directory write the following command.
1 |
cp filename.txt /MyDirectory/MySubDir/Folder1/ |
-r
To copy one directory into other directory use the -r option, which means recursively copy all the files.
(Copy directory1 into directory2)
1 |
cp -r directory1 directory2 |
-i
Just as you saw while deleting files with rm, -i will explicitly ask you before copying every file. This is useful in the case where you want to copy only some of the files from one directory to the other.
1 |
cp -r -i directory1 directory2 |
-v
The v means verbose, if you add this option then it will output a summary of all the files that have been copied.
1 |
cp -v -r directory1 directory2 |
MV(move)
To move a file to another directory write the following command.
1 |
mv filename.txt MyDirectory/MySubDir/Folder/ |
You can also rename a directory using the mv command.
1 |
mv OldName NewName |
To move a directory into another directory.
1 |
mv directory1 directory2 |
(Remember if directory2 doesn’t exist then directory1 will be renamed to directory2)
-i
When you use the option -i, mv command, while moving files, will ask you confirmation for overwriting a file whenever there is a file already existing with the same name.
1 |
mv -i directory1 directory2 |
With these linux commands you have enough power to play around with files. Again I would urge you to try these commands on your own, practice to get a good hold onto them. Create a temporary directory and start messing around with it using these commands.
Previous Tutorial : Getting Familiar with Linux Commands – Tutorial 0 – Basic Commands
0 Comments