Basic Linux Commands
12 min read
New Linux converts coming from the Windows world may find working with the command line to be somewhat intimidating. However, it’s not that difficult to use. All you need to get started with the command line is to learn a few basic commands.
While most Linux distributions are user-friendly and come with an easy to use graphical interface, knowing how to use the command line can be very useful. The command line gives you more power over your system and access to features that are not available through a graphical interface.
In this article, we’ll go through some of the most common Linux commands that are used on a daily basis by Linux system administrators.
Getting Information About the Command
Memorizing command options is usually not necessary and maybe a waste of time. Usually, if you are not using the command frequently, you can easily forget its options.
Most commands have a
--help option, which prints a short message about how to use the command and exits:
Almost all Linux commands are distributed together with man pages. A man or manual page is a form of documentation that explains what the command does, examples of how you run the command, and what arguments it accepts.
man command is used to display the manual page of a given command.
For example, to open the man page of the,
cd command, you would type:
To navigate the man pages, use the
Page Up, and
Page Down keys. You can also press the
Enter key to move one line at a time, the
Space bar to move to the next screen, and the
b key to go one screen back. To exit the man page, press the
Navigating the File System
In Linux, every file and directory is under the root directory, the first or top-most directory in the directory tree. The root directory is referred to by a single leading slash
When navigating the file system on operating on files, you can use either the absolute or the relative path to the resource.
The absolute or full path starts from the system root
/, and the relative path starts from your current directory.
Current Working Directory (
The current working directory is the directory in which the user is currently working. Each time you interact with your command prompt, you are working within a directory.
pwd command to find out what directory you are currently in:
The command displays the path of your current working directory:
Changing directory (
cd (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems.
When used without any argument,
cd will take you to your home directory:
To change to a directory, you can use its absolute or relative pathname.
Assuming that the directory
Downloads exists in the directory from which you run the command, you can navigate to it by using the relative path to the directory:
You can also navigate to a directory by using its absolute path:
Two dots (
..), one after the other, represent the parent directory or, in other words, the directory immediately above the current one.
Suppose you are currently in the
/usr/local/share directory. To switch to the
/usr/local directory (one level up from the current directory), you would type:
To move two levels up:
To change back to the previous working directory, use the dash (
-) character as an argument:
If the directory you want to change to has spaces in its name, you should either surround the path with quotes or use the backslash () character to escape the space:
cd Dir\ name\ with\ space
Working with Files and Directories
Listing directory contents (
ls command list sinformation about files and directories within a directory.
When used with no options and arguments,
ls displays a list in alphabetical order of the names of all files in the current working directory:
To list files in a specific directory, pass the path to the directory as an argument:
The default output of the
ls command shows only the names of the files and directories. Use the
-l to print files in a long listing format:
ls -l /etc/hosts
The output includes the file type, permissions, number of hard links, owner, group, size, date, and filename:
-rw-r--r-- 1 root root 337 Oct 4 11:31 /etc/hosts
ls command doesn’t list the hidden files by default. A hidden file is any file that begins with a period (
To display all files, including the hidden files, use the
ls -a ~/
Displaying file contents (
cat command prints the contents of one or more files and merge (concatenate) files by appending one file’s contents to the end of another file
To display the contents of a file on the screen, pass the file name to
cat as an argument:
Creating files (
touch command is used to update the timestamps on existing files and directories as well as to create new, empty files.
To create a file , specify the file name as an argument:
If the file already exists,
touch will change the file’s last access and modification times to the current time.
Creating directories (
In Linux, you can create new directories (also known as folders) using the
To create a directory, pass the name of the directory as the argument to the command:
mkdir can take one or more directory names as its arguments.
If the argument is a directory name, without the full path, the new directory is created in the current working directory.
To create parent directories, use the
mkdir -p Projects/linuxize.com/src/assets/images
The command above creates the whole directory structure.
mkdir is invoked with the
-p option, it creates the directory only if it doesn’t exist.
Creating symbolic links (
A symbolic link (or symlink) is a special type of file that points to another file or directory.
To create a symbolic link to a given file, use the
ln command with the
-s option, the name of the file as the first argument, and the name of the symbolic link as the second argument:
ln -s source_file symbolic_link
If only one file is given as an argument,
ln creates a link to that file in the current working directory with the same name as the file it points to.
Removing files and directories (
To remove files and directories, use the
By default, when executed without any option,
rm doesn’t remove directories. It also doesn’t prompt the user for whether to proceed with the removal of the given files.
To delete a file or a symlink, use the
rm command followed by the file name as an argument:
rm accepts one or more file or directory names as its arguments.
-i option tells
rm to prompt the user for each given file before removing it:
rm -i file.txt
rm: remove regular empty file 'file.txt'?
-d option to remove one or more empty directories:
rm -d dirname
To remove non-empty directories and all the files within them recursively, use the
-r (recursive) option:
rm -rf dirname
-f option tells
rm never to prompt the user and to ignore nonexistent files and arguments.
Copying files and directories (
cp command allows you to copy files and directories.
To copy a file in the current working directory, use the source file as a first argument and the new file as the second:
cp file file_backup
To copy a file to another directory, specify the absolute or the relative path to the destination directory. When only the directory name is specified as a destination, the copied file will have the same name as the original file.
cp file.txt /backup
By default, if the destination file exists, it will be overwritten.
To copy a directory, including all its files and subdirectories, use the
cp -R Pictures /opt/backup
Moving and renaming files and directories (
mv command (short from move) is used to rename and move and files and directories from one location to another.
For example, to move a file to a directory, you would run:
mv file.txt /tmp
To rename a file you need to specify the destination file name:
mv file.txt file1.txt
The syntax for moving directories is the same as when moving files.
To move multiple files and directories at once, specify the destination directory as the last argument:
mv file.tx1 file1.txt /tmp
Installing and Removing Packages
A package manager is a tool that allows you to install, update, remove, and otherwise manage distro-specific software packages.
Different Linux distributions have different package managers and package formats.
Only root or user with sudo privileges can install and remove packages.
Ubuntu and Debian (
Advanced Package Tool or APT is a package management system used by Debian-based distributions.
There are several command-line package management tools in Debian distributions, with
apt-get being the most used ones.
Before installing a new package first, you need to update the APT package index:
The APT index is a database that holds records of available packages from the repositories enabled in your system.
To upgrade the installed packages to their latest versions, run:
Installing packages is as simple as running:
apt install package_name
To remove an installed package , enter:
apt remove package_name
CentOS and Fedora (
RPM is a powerful package management system used by Red Hat Linux and its derivatives such as CentOS and Fedora. RPM also refers to the
rpm command and
.rpm file format.
To install a new package on Red Hat based distributions, you can use either
dnf install package_name
Starting from CentOS 8
yum as the default package manager.
dnf is backward compatible with
To upgrade the installed packages to their latest versions, type:
Removing packages is as simple as:
dnf remove package_name
File Ownership and Permissions
In Linux, access to the files is managed through file permissions, attributes, and ownership. This ensures that only authorized users and processes can access files and directories.
In Linux, each file is associated with an owner and a group and assigned with permission access rights for three different classes of users:
- The file owner.
- The group members.
- Everybody else.
Three permissions types apply to each class:
- The read permission.
- The write permission.
- The execute permission.
This concept allows you to specify which users can read the file, write to the file, or execute the file.
To view the file owner and permissions, use the
ls -l command.
Changing permissions (
chmod command allows you to change the file permissions. It works in two modes, symbolic and numeric.
When using the numeric mode, you can set the permissions for the owner, group, and all others. Each write, read, and execute permissions have the following number value:
r(read) = 4
w(write) = 2
x(execute) = 1
- no permissions = 0
The permissions number of a specific user class is represented by the sum of the values of the permissions for that group.
For example, to give the file’s owner read and write permissions and only read permissions to group members and all other users, you would run:
chmod 644 filename
Only root, the file owner, or user with sudo privileges can change the permissions of a file.
To recursively operate on all files and directories under a given directory, use the
chmod command with the -R, (–recursive) option:
chmod -R 755 dirname
Be extra careful when recursively changing the files’ permissions.
Changing ownership (
chown command allows you to change the user and group ownership of a given file, directory, or symbolic link.
To change the owner of a file, use the
chown command followed by the user name of the new owner and the target file:
chown username filename
To change both the owner and the group of a file, invoke the
chown command followed by the new owner and group separated by a colon (
:) with no intervening spaces and the target file:
chown username:groupname filename
--recursive) option, to recursively operate on all files and directories under the given directory:
chown -R username:groupname dirname
Elevate privileges (
sudo command allows you to run programs as another user, by default the root user. If you spend a lot of time on the command line,
sudo is one of the commands that you will be using quite frequently.
sudo instead of login in as root is more secure because you can grant limited administrative privileges to individual users without them knowing the root password.
sudo, simply prefix the command with
Managing Users and Groups
Linux is a multi-user system, which means that more than one person can interact with the same system at the same time. Groups are used to organize and administer user accounts. The primary purpose of groups is to define a set of privileges such as reading, writing, or executing permission for a given resource shared among the users within the group.
Creating users (
useradd command allows you can create new users.
To create a new user account, invoke the
useradd command followed by the username:
Once the user is created, set the user password by running the
Removing users (
In Linux, you can delete a user account using the
To delete a user account named pass the user name to the
-r (–remove) option to remove the user’s home directory and mail spool:
userdel -r newuser
Managing groups (
To create a new group, invoke the
groupadd command followed by the group name:
To remove a group, use the
groupdel command with the group name as argument:
Adding users to groups (
To add an existing user to a group, use the
usermod command followed by the
-G option and the name of the group:
usermod -a -G sudo linuxize
We have covered some of the most used Gnu/Linux commands.
Although you can perform most of the development and system-related tasks using a graphical interface, the command line makes you more productive and able to get more done in less time.
Click on the links on each command to get more information about the command options and usage.
If you have any questions or feedback, feel free to leave a comment.