Linux: Useful commands for daily use
Today I will go through a few useful tools that every Linux CLI (command-line interface) user will need.
Listing files and directories
ls command is a simple way to list all files and directories in a Linux system. I will list a few ways this command can help you here:
List all files and directories (file names only), stacking many on each line:
Same as above but one file or directory name on each line with other data related to the item:
This one will display information like permissions, owner, group, size (bytes) and last edited date.
If you are going to look in the size column it might be inconvenient to calculate bytes in your head, then you can make a small addition to the command above and the bytes will be translated to KB, MB, GB etc. Use the following:
You can also search recursively using this command:
Searching for files or directories using grep
grep command came up earlier when I wrote about Groups and Users, back then we searched for Linux users or groups. One of the more useful aspects of Linux command line interface is the ability to chain commands (pipe). Just combine your
ls command with
grep and you will be able to search for files and directories with ease.
Search for a specific file or directory in your current location:
ls -lh | grep ""
Keep in mind that grep is case sensitive, if you want to search without case sensitivity you just modify the command a little:
ls -lh | grep -i ""
Move back and forth in the file system
Now you know how to look around in your current location. Next up is to move around in the linux file system. To move around in the file system you can use the command
cd. This command can be used in different ways, I will list a few right here:
Go to your home directory:
Go to the root directory:
Go back one step from where you are right now:
Create and delete directories
Creating a directory is easy, use the following command:
The directory will be created at your current location when using this method.
Deleting a directory is almost as simple but there are two ways. First one is
rmdir, this command will delete empty directories only. To delete a directory (or a file) with its content we use
rm in the following ways:
Delete a directory recursively:
Delete a directory but ask before every delete:
Of course you can also combine them to delete recursively but ask before every deletion.
Moving and renaming files
Moving and renaming files can be done using the same command. This command is
mv, and I will list some useful ways to run this command:
Rename a file:
Rename a file but ask if you attempt to overwrite another file:
Reading contents of a file
The Linux operating system is working with a lot of files, most of these files are text files. I will list a few ways to read the contents of a file and where you might want to use one and not the other.
Listing contents of a file, one page at a time. You can move up and down using the arrow keys or page up/down:
This command is useful when you want to look through a file fast from start to end.
If you want to read a log file or any file that is being updated as you read it you can display all new lines added to a file using the tail command. This is useful for tracking new log entries in a log file. This is an example:
To edit a file while reading you must open the file with an editor. A simple one that comes with most Linux distributions is nano. You can edit a file using nano like this:
There are more ways to read the contents of a file in Linux but I have only listed a few different ways that fit into different needs here.
Do you think I missed something important or maybe you have some other feedback? Please contact me at email@example.com. Just add a link to this article and the e-mail will reach me.