Basic Linux Shell Commands


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

Below you’ll find a list of some basic yet useful Linux commands for the command line interface (i.e., the shell), which is the interface provided by users to interact with services and programs provided by the operating system kernel.

1. man pages

When in doubt about a specific command or program, check is manual pages. Simply write “man command_name”. For instance, “man ls” or “man cp”.

2. mkdir

Used to create new directories. For instance “mkdir images”.

3. cd

Used to change directory. Notice that the current directory is specified by . (one dot) and the parent directory by .. (two dots). So use “cd ..” to return to the parent directory.

4. pwd

This will “print working directory”. That is, it will print the full path to your current directory.

5, rm

Used to remove/delete a file or directory. Use the -r option if you want to remove sub directories and their content recursively.

6. ls

Lists the contents of the current directory. It stands for “list segments”, though this name comes from older times when segments were still used. Useful options:

-a (list all files, including hidden ones starting with .)
-l (long form, which includes all the file details)

7. ps

Lists the current processes. Usually used with the -ef option. The name stands for “process status”.

Another useful way to run ps is this:

ps -aux | less

a is for all users, u is for displaying more information about each process, and x is for displaying daemons as well. The | symbol pipes the output into another program, in this case the Less program.

8. less

Less is a pages program. It’s used to view stuff inside the terminal, one page at a time. It basically makes it easier to navigate through large files, or to find specific sections. Some basic less commands follow:

Down and Up arrows – Move up and down on the lines
B and F – Move back and forward around pages
/pattern – Searches for a pattern on the text
n – Moves to the next appearance of pattern, if one exists
g – Goes back to first line
q – Quit the program

9. ssh user@host.com

Command to connect to another host via secure shell.

10. scp

Command to upload and download files from remote servers, called secure copy.

To get a file from a remote server:
scp user@host.com:/home/user/file.zip ~/Desktop

To send a file from your local machine:
scp file.zip user@host.com:/home/user/

11. grep

Command line tool to search for text using regular expressions. For example, use this command to search for files containing the string “test”, starting at the current directory and recursively going to deeper ones.

grep -r "test" ./

12. kill

Despite the name this command is not only used to terminate processes. It’s used to send signals. If you don’t specify a signal it will send the SIGTERM (terminate), which terminates a process but might be blocked by such program (i.e., you can’t be sure it will terminate).

kill 3458 (send SIGTERM to pid 3458)

If you want to kill it for sure use the -9 or -kill signal. Other useful signals are STOP, CONT and INT.

13. find

Very useful command for finding files. If you want to find files only in the current directory that contain a certain string on their names, use this:

find -name "*string*" -maxdepth 1

You can also use the ‘locate’ command, but it has fewer features (though it’s faster).

14. chmod

This command changes the mode bits (hence the name) of files. Basically it alters the file permissions. First of all if you want to change the Owner or Group of the file use:

chmod owner:group

Second, you may change the read, write and execute permissions for three levels: owner/user, group and others. You specify the level with u, g and o, and the permissions with + or – and r, w and x. So let’s say we want to:

-give read and write permission to user/owner
-give read permission to group
-give read permission to others

we use these commands:

chmod u+rd file1
chmod g+r file1
chmod o+r file1

We can also use binary values to alter all three levels at the same time. Basically read = 4, write = 2, execute = 1, so we can do the previous settings like this:

chmod 644 file1

If you wanted to give rwx permissions to all levels you would do:

chmod 777 file1

15. cat, head, tail

These commands are used to display the content of files in the standard output (i.e., the terminal). Cat will show all the content, head the first 10 lines, and tail the last 10 lines.

16. cat

The cat (concatenate) command can be used for several other purposes. For instance, you can use it to read from standard input and output to a file:

cat > file1.txt

With this you can create and write to that file directly from the command line. Use Ctrl+D to send a “end of file” character and close it.

You can also copy the contents of one or more files to another:

cat file1.txt > file2.txt
cat file1.txt file2.txt > file3.txt

17. sort

Sorts contents of a file numerically or alphabetically. For instance:

ls | sort

18. apropos

If you can’t remember the command you are looking for use “apropos keywords”, and it will return a list of all comments that mention that keyword on the header of their man page. For instance:

apropos copy

19. <, >, |, ;, & and $

< is used to redirect the input of a command. > is used to redirect its output.
>> to redirect the output at the end of an existing file
| is used to pipe the output of a command into the input of another command.
; used to run one command after the other
& if placed after the command it will make it run in the background
$ used to substitute the output of a command into the command line itself

For example:

echo "found $(ls | wc -w) files in this directory"

20. System information

who i am – displays information about your user
who – displays information about all logged in users you can see
date – displays the date and time
uname -a – displays information about the system
echo $PATH – displays the directories included in your path (i.e., checked when commands are executed)
alias – show current shell aliases
type command – shows where a command is coming from (e.g., alias, shell built-in, file system)

21. history

Shows the history of shell commands used, with an index number. You can any of them by typing !number.

22. touch

Touch is used to alter the timestamp of a file. If the file doesn’t exit, an empty one is created.

23. {}

Curly braces will expand into many file names. For instance:

touch file{1,2,3}

Will create files file1, file2 and file3 if they don’t already exit.

You can also use ranges, as in:

touch file{1..50}

24. tr, cut, sed

tr is used to translate and delete characters in a file.
cut is used to cut specific parts of each line on a file or command output.
sed is a scriptable editor, which you can use to accomplish many text editing tasks.

Please refer to the man page of each command to get details about them.

Leave a Reply

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