Press Ctrl/Cmd + P to print
or save as PDF

7 Useful Linux Commands

7 Useful Linux Commands

By reading this guide article, you will learn about 7 Useful Linux Commands.

7 Useful Linux Commands

1) ls : list the contents

The ls command means to list the directory contents. Using it with -F will give a list of the directories contents, and denote items that are other directories with a trailing /.

ls -F

On my server returns:

allthethings.txt important.doc Indominus/ Misc/ probs.xls Red Wings/ Spreadsheets/ Work/

In the above case, allthethings.txtgarbage.fileimportant.doc, and probs.xlsare files, and IndominusMiscRed WingsSpreadsheets, and Work, each with the trailing /, are directories!

There are many other options, or switches, such as -F that can be used with ls for improved results. For example:

ls -lFa

Returns:

dr-xr-x---. 10 root root 4096 Apr 17 12:01 .
drwxr-xr-x. 19 root root 4096 Apr 14 12:45 ..
-rw-r--r-- 1 root root 0 Apr 17 12:00 allthethings.txt
-rw------- 1 root root 483 Apr 14 12:45 .bash_history
-rw-r--r--. 1 root root 18 Dec 28 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r--r--. 1 root root 361 Jan 1 01:24 .bashrc
drwxr-xr-x 3 root root 4096 Jan 1 01:25 .cache/
drwxr-xr-x 3 root root 4096 Jan 1 01:25 .config/
-rw-r--r--. 1 root root 100 Dec 28 2013 .cshrc
-rw-r--r-- 1 root root 0 Apr 17 12:01 garbage.file
-rw-r--r-- 1 root root 0 Apr 17 11:58 important.doc
drwxr-xr-x 2 root root 4096 Apr 17 11:59 Indominus/
drwxr-xr-x 2 root root 4096 Apr 17 11:57 Misc/
-rw------- 1 root root 42 Apr 14 12:44 .my.cnf
-rw-r--r-- 1 root root 0 Apr 17 12:00 probs.xls
drwxr-xr-x 2 root root 4096 Apr 17 11:57 Red Wings/
-rw------- 1 root root 1024 Jan 1 01:22 .rnd
drwxr-xr-x 2 root root 4096 Apr 17 11:56 Spreadsheets/
drw------- 2 root root 4096 Apr 14 12:42 .ssh/
-rw-r--r--. 1 root root 129 Dec 28 2013 .tcshrc
drwxr-xr-x 2 root root 4096 Apr 17 11:57 Work/

In the above case two switches are added: -l and -a. The -l uses the long listing format, and the -a switch lists all of the files, including hidden files.

Each column contains an important bit of information:

Column | Information | Example

  • 1 | Permissions | drwxr-xr-x
  • 2 | # of Hard Links | 2
  • 3 | User That Owns File or Directory | root
  • 4 | Group for File or Directory | root
  • 5 | File Size | 4096
  • 6 | Timestamp | Apr 17 11:59
  • 7 | Filename | Indominus/

2) pwd : current directory

Before deleting anything, it’s always helpful to know what directory you’re in; the pwd command will tell you and stands for present working directory:

pwd

Returns the full pathname:

/home/dinosaursareawesome

Checking the present working directory can prevent errors when you’re using commands that will remove files or directories… in fact, it can save you from a lot of stress and headache!

3) cd : change directory

The cd command stands for change directory and is used extremely frequently.

For example, if you’re currently in your home directory /home/dinosaursareawesome:

pwd

/home/dinosaursareawesome

but want to change to another directory within that directory… say… Velociraptor:

ls -laF

drwxr-xr-x 3 root root 4096 Apr 17 16:44 ./
drwxr-xr-x. 4 root root 4096 Apr 17 16:25 ../
drwxr-xr-x 2 root root 4096 Apr 17 16:44 Velociraptor/

Then you can use the following command to change to Velociraptor:

cd Velociraptor/

And then verify that you made it to the right directory:

pwd

/home/dinosaursareawesome/Velociraptor

4) touch : create file

The touch command is generally used to change file timestamps, but it can also be used for creating a new file.

Let’s look at changing a time stamp first. In the case below, I have a file named Foxtrot in the current directory, that was created on Apr 14 12:46:

ls -laF

drwxr-xr-x 2 root root 4096 Apr 17 16:54 ./
drwxr-xr-x 3 root root 4096 Apr 17 16:44 ../
-rw-r--r-- 1 root root 5262 Apr 14 12:46 Foxtrot

Now, let’s say I wanted to update that timestamp, Apr 14 12:46 to the current timestamp, but without changing the file. Simply touch the file:

touch Foxtrot

And then check the new timestamp and you’ll see it has been updated to the current time, which for me is Apr 17 17:01:

ls -laF

drwxr-xr-x 2 root root 4096 Apr 17 16:54 ./
drwxr-xr-x 3 root root 4096 Apr 17 16:44 ../
-rw-r--r-- 1 root root 5262 Apr 17 17:01 Foxtrot

The second use of the command is to create a file. Let’s say I wanted to create the file Golf in my present working directory:

touch Golf

Now look for your new file:

ls -laF

drwxr-xr-x 2 root root 4096 Apr 17 17:04 .
drwxr-xr-x 3 root root 4096 Apr 17 16:44 ..
-rw-r--r-- 1 root root 5262 Apr 17 17:01 Foxtrot
-rw-r--r-- 1 root root 0 Apr 17 17:04 Golf

5) history : show all commands that have been recently used

The history command will print a history of all the previously executed commands by the current user. For example:

history

95 20150417 - 17:01:48 - touch Foxtrot
96 20150417 - 17:01:50 - ls -laF
97 20150417 - 17:04:06 - touch Golf
98 20150417 - 17:04:07 - ls -la

6) uname : show details about the OS

The uname command is most commonly used to determine which OS you’re running, its version, and the kernel version. With the -a switch, it will show: 1. kernel name, 2. network node hostname, 3. kernel release, 4. kernel version, 5. machine hardware name, processor type or “unknown”, 6. hardware platform or “unknown”, and 7. the operating system.

uname -a

Linux centos7core.thebestfakedomainnameintheworld.com 3.10.0-123.13.2.el7.x86_64 #1 SMP Thu Dec 18 14:09:13 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

  • Kernel Name: Linux
  • Network Node Hostname: centos7core.thebestfakedomainnameintheworld.com
  • Kernel Release: 3.10.0-123.13.2.el7.x86_64
  • Kernel Version: #1 SMP Thu Dec 18 14:09:13 UTC 2014
  • Machine Hardware Name: x86_64
  • Processor Type: x86_64
  • Hardware Platform: x86_64
  • Operating System: GNU/Linux

7) man : help pages

The man command opens manual pages for individual commands; basically, it’s your access to quick information on any command. For example, if you wanted to read the manual page for ls, then you would run:

man ls

That’s it!

Read the next article-> 5 Commands to check Linux Memory Usage.