Basic Linux Commands for Beginners

Basic Linux Commands for Beginners

Basic Linux commands for beginners with most frequently used patterns and examples.

In this article, basic Linux commands with their most frequent usages are outlined in alphabetical order for beginners. Please note that, we aim to keep the commands and the examples as simple as possible to aid in learning for beginners rather than giving full descriptions of the commands. To explore more on the commands , readers are advised to read the command manuals by $ man command.

What is Linux?

Linux is a family of open-source operating systems based on the Linux kernel. The first Linux system kernel was released on September 17, 1991, by Linus Torvalds.

Read more …

Popular Linux distributions include Debian, Fedora, and Ubuntu, and the commercial distributions include Red Hat Enterprise Linux and SUSE Linux Enterprise Server.

There are also quite a number of customized Linux distributions, such as Kali Linux, REMnux etc. Kali Linux is a Debian-based distribution developed, funded and maintained by Offensive Security for ethical hackers for the purposes of Penetration Testing, Security Research & Assessment, and Computer Computer Forensics & Reverse Engineering. REMnux, on the other hand, is a Linux distro curated for reverse-engineering and malware analysis purposes.

Quote by Dennis Ritchie
Quote by Dennis Ritchie

UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.

Dennis Ritchie

Read more educational and inspirational cyber quotes at our page 100+ Best Cyber Security & Hacker Quotes.

Basic Linux Commands

adduser, addgroup – Add a user or group

Add a user or group to the system. Basic usage: adduser [OPTION] ... or add group [OPTION] ...
$ adduser

cat – Concatenate and display files

Concatenate FILE(s) to standard output. Basic usage: cat [OPTION] ... [FILE] ... 
$ cat filename
Displaying Contents of a File with the cat Command

cd – Change the shell working directory

Change the current shell working directory to DIR. Basic usage: cd [DIR] The default DIR is the value of the $HOME. So, just typing cd without parameters (or cd ~) changes the current directory to /home. Also, use - as a parameter to return back to the previous directory or .. to switch to the parent directory. 
$ cd /home/username
$ cd -
$ cd ..

chmod – Change file mode bits

Change file permissions by defining mode bits. Basic usage: chmod [MODE][,MODE] ... FILE ... 

The format of a symbolic mode is [ugoa][[-+=][PERMS ...]] A combination of letters defines which users’ permissions will be changed: the user who owns it (u), other users in the file’s group (g), other users not in the file’s group (o), or all users (a). The letters rwx select file mode bits for the affected users: read (r), write (w), execute (x).

A numeric mode is from one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1. The second digit selects permissions for the user who owns it, the third digit selects permissions for the users in the file’s group, and the fourth digits selects permissions for the other users not in the file’s group. The digits that can be used and their permissions are:

  • 0 (000) No permission
  • 1 (001) Execute
  • 2 (010) Write
  • 3 (011) Write and Execute
  • 4 (100) Read
  • 5 (101) Read and Execute
  • 6 (110) Read Write
  • 7 (111) Read, Write and Execute

To add execute permission for everyone:

$ chmod a+x filename

To give read, write and execute permissions to the file’s owner, read permission to the file’s group and no permissions to all other users:

$ chmod u=rwx,g=r,o= filename
$ chmod 740 filename

chown – Change file owner and group

For more examples on how to use the chmod command with different options, you could visit File Permissions and chmod Command in Linux.

Change the user and/or group ownership of each given file. Basic usage: chown [OPTION][:[GROUP]] FILE ...
$ chown username:groupname filename

cp – Copy files and directories

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. Basic usage: cp [OPTION] SOURCE DEST or cp [OPTION] SOURCE ... DIRECTORY For instance, to copy a file from the current working directory to /home/newdir:
$ cp filename /home/newdir

echo – Display arguments to the screen

Echo the STRING to the standard output . Basic usage: echo [STRING] ... Echo command can also be used to display environment variables by prefixing them with the $ symbol.
$ echo linux is fun
$ echo $PATH
Using echo Command to Display PATH Variable

env – Run a program in a modified environment

Set each VALUE to VARIABLE in the environment and run COMMAND. Basic usage: env [OPTION] ... [VARIABLE=VALUE] ... [COMMAND] If no COMMAND is specified, used to set environment variables. Use -i to ignore environment variables, i.e., start with an empty environment. 
$ env -i /bin/sh

To learn more on what environment variables are and how to set/unset them, you could visit Linux Environment Variables.

export – Export attribute to the variables

Set an ATTRIBUTE to the defined environment variable (ENV-VAR) . Basic usage: export [ENV-VAR[=ATTRIBUTE]] ... For instance, to set vim to the EDITOR environment variable:
$ export EDITOR=/usr/bin/vim

find – Search for files

Search for files in a directory hierarchy rooted at each given STARTING-POINT by evaluating the given EXPRESSION. Basic usage: find [STARTING-POINT ...] [EXPRESSION] Use -name to specify file name, -perm to specify file permissions,  and -user to define owner of the file searched for. 
$ find / -name filename -user username

Use -ls to apply ls command on each of the found items, and -exec command {} \; to run command against all the files that are found.

$ find / -name filename -ls
$ find / -name filename -exec ls -la {} \;

To search for the files that are owned by the root user and have their setuid bit set, you can use the following commands.

$ find / -user root -perm -u=s -ls 2> /dev/null
Default File Permissions for the /usr/bin/sudo Command

For more examples on how to use the find command with different options, you could visit How to Search for Files in Linux.

grep – Print lines that match patterns

Search for PATTERNS in each FILE. Basic usage: grep [OPTION] ... PATTERNS [FILE] ... Use -c to suppress normal output and print a count of matching lines instead, use -i to ignore case distinctions in patterns, -n to prefix each line of output with a line number, and -v (invert match) to select non-matching lines.
$ grep -n error /var/log/boot.log

head – Output the first part of files

Print the first 10 lines of each FILE to standard output. Basic usage: head [OPTION] ... [FILE] ... Use -n to specify the number of lines printed.
$ head -n 5 file

kill – Send a signal to a process

Send a signal to a process. The default signal for kill is TERM. Basic usage: kill [OPTION] PID Use -l to display all the available signals.
$ kill PID

ln – Make links between files

Create a link to TARGET with the name LINK. Basic usage: ln [OPTION] ... TARGET LINK  Use -s to make symbolic links instead of hard links. Default is hard links (when -s is not used), where two separate copies exists and any change in one of them is reflected on the other. Note that, TARGET should exist, and LINK should not (it is created automatically).
$ ln -s filename /home/newdir/symlinkfile

ls – List directory contents

List information about the FILE(s) (the current directory by default). Basic usage: ls [OPTION] ... [FILE] ... Use -a to list all entries (including the hidden ones that start with .) and -l to list with more details, such as file permissions, file size etc.
$ ls -la
Listing Directory Contents with the ls Command

For more examples on how to use the ls command with different options, you could visit ls Command in Linux.

man – Display reference manual

Display manual pages for items, such as commands. Basic usage: man term For instance, to display the manual page for the program ls:
$ man ls

mkdir – Make directories

Create DIRECTORY(ies), if they do not already exist. Basic usage: mkdir [OPTION] ... DIRECTORY ... Use -v to print a message for each created directory, or use -p to create a hierarchy of DIRECTORY(ies).
$ mkdir -v dir1
$ mkdir -p dir1/dir2/dir3
Creating a Directory Hierarchy with the mkdir -p Command

more – Filter for paging through text

Filter for paging through text one screenful at a time. one Basic usage: more [OPTION] FILE Use -n to specify the number of lines per screenful.
$ more filename

mv – Move (rename) files

Move (rename) SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. Basic usage: mv [OPTION] SOURCE DEST or mv [OPTION] SOURCE ... DIRECTORY For instance, to move a file from a given directory to one directory up by changing the file name:
$ mv /home/filename1 ../filename2

printenv – Print all or part of environment variables

Print the values of the specified environment VARIABLE(s). The default is to print all, if no environment VARIABLE is provided. Basic usage: printenv [OPTION] ... [VARIABLE] ... Use -u to unset a variable.
$ printenv PATH

ps – Display current processes

Display information about a selection of the active processes. Basic usage: ps [OPTION] ... Use -e to select all processes, -f to full format listing, and -H to show process hierarchy (forest). To display running processes as tree without additional information, use command pstree.
$ ps -efH
$ pstree

pwd – Print name of the present directory (Print Working Directory)

Print the full filename of the current working directory. Basic usage: pwd [OPTION] ...
$ pwd

rm – Remove files or directories

Remove specified FILE(s). Basic usage: rm [OPTION] ... [FILE] ... Use -f to ignore prompts , -r to remove directories and their contents recursively and -v (verbose) to be informed about what is being done.
$ rm -frv /home/newdir

su – Run a command with substitute user

Run a command with a substitute user and group ID. Basic usage: su [USER] When no user is specified, su defaults to running an interactive shell as root.
$ su username

sudo – Execute a command as another user

Execute a command as the superuser or another user. Basic usage: sudo [-u USER] FILE ... The sudoers security policy (/etc/sudoers) defines if a user can run sudo and with which privileges. 
$ sudo cat /etc/shadow

tail – Output the last part of files

Print the last 10 lines of each FILE to standard output. Basic usage: tail [OPTION] ... [FILE] ... Use -n to specify the number of lines printed, and -f to output appended data as the file grows.
$ tail -n 5 filename
$ tail -f filename

top – Display current processes

Display a dynamic real-time view of a running system. Basic usage: top [OPTION] ... Use -u to specify a user and -p to specify a process id (PID).
$ top

touch – Change file timestamps

Update the access and modification times of each FILE to the current time. Basic usage: cat [OPTION] ... FILE ... If the FILE does not exist, an empty file is created.
$ touch filename

uname – Print system information

Print certain system information, such as, kernel name, kernel version, operating system etc. Basic usage: uname [OPTION] ... Use -a to print all information.
$ uname -a

usermod – Modify a user account

Modify user account. Basic usage: usermod [OPTION] USER Use -a to add the user to the supplementary groups specified with the -G option.
$ usermod -a -G groupname username

wc– Word count for each file

Print new line, word, and byte counts for each FILE, and a total line if more than one FILE is specified. Basic usage: wc [OPTION] ... [FILE] ...
$ wc filename
Displaying Line, Word, and Byte Counts with the wc Command

whoami – Print effective userid

Print the user name associated with the current effective user ID. Basic usage: whoami [OPTION] ...
$ whoami

To learn more on Linux, you could also visit our Linux Resources Page.