Home » Software » Basic Linux Command Line (CLI)

Basic Linux Command Line (CLI)

The command-line interface, known as CLI, is an interface that allows you to type in text commands, telling the computer to carry out specific tasks. This is direct contrast to clicking the mouse on a menu or button to command the computer. Your computer can be controlled almost entirely by typed text commands and, in many cases, this speeds up many processes. You also have the option of automating some tasks with special commands that are set on a loop and will perform the same task on different files, saving you a ton of time.

The interface where you type in your text commands is called a shell – there are loads of different ones that you can choose between but the most commonly used one is called Bash and is the default shell on Linux and Mac OS X systems. If you are using a Desktop environment for this, you will need a terminal emulator. Some environments have their own terminal emulators, mostly Konsole in KDE and GNOME. These emulators will pop up a Terminal window for you use.

Basic Commands

Opening the terminal emulator, you will see the name of the user who is currently logged in to the system, along with the hostname. If you see “$” it means you are logged in as a normal user, whereas if you see “#” you are logged in as root.

Basic-Linux-Command Basic Linux Command Line (CLI)You should not be logged in as root unless you are performing administrative tasks or are working within the root directories. If you are logged in as root it changes all the permissions for every directory and the files within these directories which means the rot will be the user and you will not be able to gain access when you are logged in as a normal user.

So, let’s get started with a few commands. You have your terminal window open and are ready to go. I am going to list the most basic commands, depending on the type of activity that you might want to perform. When you open your terminal, the common ad prompt will be pointing to a certain folder on your hard drive. With commands, you can navigate between individual folders, do things with the files inside the folders or lots of other actions.

List Files

Let’s begin by displaying a list of the files that are in the folder pointed to by your command prompt – this is the “active folder”. For this, you are going to use the ls command. There are lots of different parameters that you can add to the command to control what is displayed or how the information is sorted, for example:

  • Adding “-1” to the end of your command will display a detailed listing
  • Adding “-t” will give me a list sorted by file time
  • Adding “-s” gives a list sorted by file size
  • Adding “-r” gives a reversed list

Do not add the “” marks in any of the commands!

You can use any combination of command parameters together. This example will sort the files by size, with the largest last:

“ls –lSr”

If you use another parameter – “-a” – you will be shown hidden files and you should also spot something else in the list – two different beginning entries:

  • “.” means the current folder
  • “..” means the parental folder

Change Directories

Using the cd command you can flick between directories and, if you use what we just mentioned above, you can use this command to move to the directory above the one that is currently shown: cd ..

You can move between full or relative paths as well. The command in the example above goes to a relative path – the one above the current folder. Let’s say you are currently in /path/to and you to want to go to a folder called stuff, simply type cd stuff

You can also move to absolute paths – again, let’s say you are in /path/to/ and you want to go to “/another/path/, you would type:

cd /another/path/

If you wanted to swap directories with the one you were previously working in, you would use the “-“ shortcut. Let’s say we start in /first/folder/path/ and you want to switch back to /etc/ to make some changes to the configuration. Instead of typing the full path to go back, simply type in the hyphen command to take you back to the previous directory:

cd –

Creating or Removing Folders

If you want to create a new folder you would use the mkdir <foldername>  command – insert the new file name where it says <foldername>

If you want to remove a particular directory, use the rmdir <foldername>  command – again, inserting the correct file name. If the folders you want to delete have files inside, you will need to delete the files first.

Creating and Removing Files

Using the touch <foldername>  command, you can make new blank files and, to delete them, use rm <foldername>

If you wanted to remove all of the files within a directory, you can use the “*” wildcard. This is a very simple yet handy tool that will be very useful to you on the command line. Let’s say you are inside a folder and you want to get rid of all the files that are inside it – all you do is type:

rm *

If you want to delete a whole list of folders and files, including the files that are in subdirectories, without having to be prompted for each individual entry, you can use two commands – -r and –f – these are recursive and forcive options and will remove each instance of matching filename patterns:

rm –rf filemame.*

Edit Plain Text Files

If you want to edit text files, the commands will differ, depending on the platform you are using and the application. If you are doing this through Linux, you use the nano editor:

nano /path/to/file/

If you are using anything other than Linux, you can use the vim editor, which is invoked using

vi <filename>

Displaying Files

If you want to display the contents of a file on to the screen, you can use the cat command. However, the results will be too large for you to see properly. Instead, use the more or less commands

more <filename>

This will show you the contents of the file and will prompt you to scroll through a screen at a time.

Command Redirection

Every command line application accepts standard input and will write to standard output. If you want to redirect output from command to another command, you can use the > or | operators. This lets you chain commands together to make commands that are more powerful.

For example, if you wanted to us the ls –l command to show a file list, but it keep scrolling out of sight, you use the output from that command and put it into the input of the more command:

ls –l | more

If you want to save the output into a file instead of showing it on the screen, use the > operator:

ls -l > filename.list

After that, you could use cat to show the contents of the file, direct it into the grep command and redirect it to another file

cat filename.list | grep keyword > filefound.list

Running a Script in the Current Folder

If the folder you are in has a shell script or an application, you can’t expect it to open by just typing in the command name. In front of the command, you need to add “./”. This is because, in shell, the current directory or the .folder is not included in the path so, for example, if you wanted to open scriptname.sh from your current folder, you would need to type in:


Using History

The history command will display a list of all the commands you used recently. If you use CTRL+R, this will bring up a search mode – type in the first couple of letters of any command to search your recent history. You can use the up or down arrow keys to scroll through the results.

Looping Over a Set of Files

If you wanted to loop though a specific set of files and perform a set action on each separate one, you would use the for command. For example, if you wanted to loop the all .txt files that are in the current directory and then display them on the screen, you would type:

for f in *.txt;do echo $f;done

Find Files

The find command is extremely powerful and is used to find files. Let’s say you want to locate all .txt files that were modified in the last 7 days, you would type:

find . –name “*.txt” –mtime 7

Find a Text String in Files

If you want to find specific text within files, even in files that are within subdirectories, you can use the grep command. For example, if you want to look for “text string” in all files in your current directory and the directories below it, you would type:

grep –ir “text string” *

Batch Rename Files

If you want to rename some file using a regular expression pattern, you can use the rename command. For example, if you wanted to rename all the files that contain foo to contain bar instead, you would use a similar command to this:

rename –v ‘s/foo/bar/g’ *

Using Bash Shortcut Keys

There are some highly useful shortcut keys that you can use in Bash shell and it would serve you very well to learn them all. Here are some of the more common ones to get you going:

  • Ctrl + U – Clears the text off the line from the cursor backwards
  • Ctrl + A – Brings the cursor point to the start of the line
  • Ctrl + E – takes the cursor to the end of the line
  • Ctrl + R – Lets you search previous commands

Using Aliases

Aliases are brilliant for saving you a great deal of time. They are used for shortening complicated lengthy commands into simple ones. You can also use them to set parameters to a command so you don’t need to type them in every single time. Let’s say you want to set an alias for when you install packages on your Linux setup, the original command would be sudo apt-get install packagename. You can shorten it by typing in:

alias agi=’sudo apt-get install’

Now when you want to install a package, you would simply type agi packagename at the command. If you want to change the default argument for a command – example, you want ls to ls –l, you would type in:

alias ls=’ls –l’

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *

This site uses Akismet to reduce spam. Learn how your comment data is processed.