Wesbytes Knowledge Base

Search our articles or browse by category below

7 Useful Linux Commands

Last modified: July 2, 2022
You are here:
Estimated reading time: 3 min

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

List the contents: ls

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

It will list the output below:

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 Files or Directory
root
4
Group for File or Directory
root
5
File Size
4096
6
Timestamp
Apr 17 11:59
7
Filename
Indominus/

Current Directory: pwd

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

Output:

/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!

Change Directory: cd

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

Output:

/home/dinosaursareawesome

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

ls -laF

Output:

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

Output:

/home/dinosaursareawesome/Velociraptor

Create File: touch

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

Output:

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

Output:

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

Output:

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

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

Output:

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

Show Details About the OS: uname

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

Output:

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

Help Pages: man

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
Was this article helpful?
Dislike 0
Views: 30