Linux Commands – File Operations – Continued


Renaming files

There is no special command to rename a file or directory. But we can use mv to do the same.

Copying and Removing directories

Using -R option we can apply cp and rm to directories also.

Using -f in case of rm forces the delete.

Example


vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public     Ubuntu One
Documents  examples.desktop  Pictures  Templates  Videos
vishnu@bios-11:~$ mkdir test
vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public     test        Videos
Documents  examples.desktop  Pictures  Templates  Ubuntu One
vishnu@bios-11:~$ mv test/ temp
vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public  Templates   Videos
Documents  examples.desktop  Pictures  temp    Ubuntu One
vishnu@bios-11:~$ cp -R temp Documents/
vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public  Templates   Videos
Documents  examples.desktop  Pictures  temp    Ubuntu One
vishnu@bios-11:~$ rm temp
rm: cannot remove `temp': Is a directory
vishnu@bios-11:~$ rm -Rf temp
vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public     Ubuntu One
Documents  examples.desktop  Pictures  Templates  Videos
vishnu@bios-11:~$ cd ./Documents/
vishnu@bios-11:~/Documents$ ls
blog  docs  learn  task list  temp
vishnu@bios-11:~/Documents$ rmdir temp
vishnu@bios-11:~/Documents$ ls
blog  docs  learn  task list
There are a lot of options for almost every command. So it is a good practise to read the man page of every new command we learn 🙂