This will be the fourth post in the Linux Series and It will be all about managing files and directories in the filesystem. This includes creating, deleting and modifying the files and moving them to other directories etc.
Managing files and directories
rm - Remove a file or dir (directory)
This command allows the user to remove a file. You simply type rm with the filename as the argument and it will remove the file.
root@User:/tmp$ ls
file
root@User:/tmp$ rm file
root@User:/tmp$ ls
Keep in mind that it will not ask you if you realy want to delete it or not so be careful with it. and it will not let you delete a directory even if that directory empty. You have to provide a flag or option -R
before the dirname to remove it.
root@User:/tmp$ ls
old-dir
root@User:/tmp$ rm -R old-dir
root@User:/tmp$ ls
cp - Copy a file or dir (directory)
This command allows the user to copy a file and directory from thr target to the destination. It is used like this. You type the cp
command and type the file you want to copy as the first argument and the destination directory as the second argument and hit enter.
root@User:/tmp$ ls
file dir
root@User:/tmp$ cp file dir
root@User:/tmp$ cd dir
root@User:/tmp/dir$ ls
file
mv - Move a file or dir
This command moves a file or dir to a new location that is provided as the second argyment. It works like this.
root@User:/tmp$ ls
some-dir file.txt
root@User:/tmp$ mv file.txt some-dir
root@User:/tmp$ ls
some-dir
root@User:/tmp$ cd some-dir
root@User:/tmp/some-dir$ ls
file.txt
mv
command can also be used to remane a file.
root@User:/tmp/some-dir$ ls
file.txt
root@User:/tmp/some-dir$ mv file.txt new-name.txt
root@User:/tmp/some-dir$ ls
new-name.txt
rmdir - Remove a dir
rm
command can delete a dir but you have to give it a -R
flag and there is also a command that is more suitable for the task. rmdir
command removes a dir only if its empty. It also ask for permission to delete a dir that contains even a single file.
root@User:/tmp/some-dir$ ls
empty-dir not-empty-dir
root@User:/tmp/some-dir$ rmdir empty-dir
root@User:/tmp/some-dir$ ls
not-empty-dir
root@User:/tmp/some-dir$ rmdir not-empty-dir
rmdir: failed to remove 'not-empty-dir': Directory not empty
Same goes for rmdir
where you have to remove a dir with some files you have to provide -r
option to remvoe it. But always be careful when working with directories.
Some more useful commands
Here are some more commands that will be useful as you work with the terminal.
Checking who is logged in with whoami
it prints out the username of the currenty logged in user which in this case is root.
root@User:/$ whoami
root
Finding path to a binary
root@User:/$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
also
Next post will be about finding file and performing searches and filtering using one of the most helpful and useful commands in linux, grep
.