Getting Familiar with Linux Commands – Tutorial 1 – File Specific Commands Gurleen Sethi July 8, 2017 Linux, Shell 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! Shell touch filename.txt 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. Shell mkdir MyDirectory 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. Shell rmdir MyDirectory 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. Shell rm filename.txt 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. Shell rm -r MyDirectory 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. Shell rm -i -r MyDirectory 1 rm -i -r MyDirectory Output Shell examine files in directory MyDirectory? y remove MyDirectory/file1? n remove MyDirectory/text.tst? y remove MyDirectory? n 1234 examine files in directory MyDirectory? y remove MyDirectory/file1? nremove MyDirectory/text.tst? yremove 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! Shell rm -f -r MyDirectory 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. Shell rm -v -r MyDirectory 1 rm -v -r MyDirectory Output Shell MyDirectory/song.mp3 MyDirectory/test.txt MyDirectory 123 MyDirectory/song.mp3MyDirectory/test.txtMyDirectory CP(copy) To copy a file from to a directory write the following command. Shell cp filename.txt /MyDirectory/MySubDir/Folder1/ 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) Shell cp -r directory1 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. Shell cp -r -i directory1 directory2 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. Shell cp -v -r directory1 directory2 1 cp -v -r directory1 directory2 MV(move) To move a file to another directory write the following command. Shell mv filename.txt MyDirectory/MySubDir/Folder/ 1 mv filename.txt MyDirectory/MySubDir/Folder/ You can also rename a directory using the mv command. Shell mv OldName NewName 1 mv OldName NewName To move a directory into another directory. Shell mv directory1 directory2 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. Shell mv -i directory1 directory2 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 Leave a Reply Cancel Reply Your email address will not be published.CommentName* Email* Website Please enter an answer in digits:three × 3 = Notify me of follow-up comments by email. Notify me of new posts by email.