Useful Command line tools

Once the domain of those who enjoyed the command line interface and the config file, general linux distributions have moved on, embracing the Graphical User Interface, trying to relate to the masses.

Command line tools still exist, however, and will continue to be used as a powerful feature in Linux, just as they are in Windows. Here are some commands that we find useful, and that just scratch the surface of the Linux command line ability.

Delete Everything

Here is a command to use with great caution.

rm -R *

This removes all files in the current directory and recurses into directories below the current one (the purpose of the -R argument). All directories will be removed once empty as well. A good command for removing a backup or archive you no longer need, a bad command to run in the root directory.

Taking ownership

If you have files that you want to take ownership of, or give ownership to another user use the following

chown user:group -R *

Again this recurses,  changing all files and directories below the current one.

Changing permissions

After you’ve change a user or group as above you may want to change the file permission so all users within a group can use the files.

chmod 775 -R *

will recusively modify the permissions on the files. 775 is an octal representation of the file permissions, Google man chmod for more info.

You can also combine chmod with the find command to change just the file or just the directory permissions recursively.

sudo find . -type f -print0 | xargs -0 sudo chmod 664

chmod recursively on directories only

sudo find . -type d -print0 | xargs -0 sudo chmod 775

Delete all files older than four months

On our servers we archive files that are deleted from Windows PCs on our Samba shares into a trash directory. This means that if you accidentally delete something you can recover it, but also means that lots of space is used up if you delete something you really do want to get rid of. The following command searches for and deletes files that were last used more than 120 days ago.

find path -mtime +120 -exec rm '{literal}{}{/literal}' ';'

The -mtime +120 argument defines how long ago we are searching for, while the -exec rm argument defines what command we want to run. You can use

find path -mtime +120 -ok rm '{literal}{}{/literal}' ';'

to test that you are deleting the right files, the -ok rm argument asking you if you really want to delete them. Hit <CTRL>-<C> to cancel the command when you know it’s doing the right thing and then use the -exec version, which executes the command without prompting you.

Compress the content of a directory

Useful for moving lots of files around or making quick backups is the tar command. To compress the content of a directory into an archive

tar -czf /destination/archive.tar.gz /source/directory/*

For more details (such as the -xf option for extracting files)

man tar

How much disk space do I have or how much am I using?

df -h

gives you a summary of disk usage.

du -sh *

list the space used by the content of each directory in the current directory.

Other tools

Remember

sudo

for running a command as the root user, as long as you have permissions on the system you are working on to do it, and

nano

as a text-mode editor.