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

The 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: ls

Same as above but one file or directory name on each line with other data related to the item: ls -l
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: ls -lh

You can also search recursively using this command: ls -R

Searching for files or directories using grep

The 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: cd ~

Go to the root directory: cd /

Go back one step from where you are right now: cd ..

Create and delete directories

Creating a directory is easy, use the following command: mkdir  
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: rm -r

Delete a directory but ask before every delete: rm -i

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: mv

Rename a file but ask if you attempt to overwrite another file: mv -i

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: less  
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: tail -f

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: nano

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 info@papershards.com. Just add a link to this article and the e-mail will reach me.

Linux: Groups and users

Linux users are important to any linux distribution, without a user you wont be able to administer your linux server at all. In this article I will go through the basics of linux users and groups.

A typical day of a web developer

Hi, my name is Jonathan, today I will tell you about a typical day of a web developer from my perspective.

Working as a Support Technician

Hello! My name is Emy and I work as a full-time support technician at a web bureau/web hosting company in southern Sweden. This article will give you.

Unity 5: Click to move with pathfinding

In this article I will show you one way to set up a simple click to move with pathfinding in Unity 5. We will setup an environment to test the pathfinding and write code required.