fbpx
Basic Commands in Linux

Basic Commands in Linux (2021 Update)

For many, the command line interface is something viewed with much dread and aversion. It seems like something only those with computer science degrees would consider using. However, the command line interface (or CLI), is an extremely powerful tool. CLI skills can speed up your workflow. Many tools not available on a computer’s graphical interface are available on the CLI.

While there’s some truth that one can do serious damage to a computer by recklessly inputting commands, there is much value in knowing your way around the command line. For one thing, if you are using a Unix-like operating system (such as a Mac or Linux computer), most of these commands will be universal. Additionally, pretty much every web server in existence uses some flavor of Linux, so these commands will apply there too. Knowing how to work with a server on the command line is often a basic prerequisite for doing any sort of back-end development.

The full list of Unix commands, with all its options is quite long and potentially intimidating to the novice. There are however, only a handful of commands you would need to know to make use of the CLI in an effective way.

This post will outline the most common and useful commands that one would likely use on a regular basis.

ls – List Files

You can use ls to list the files in the current directory (folder) you are in.

There are a few options available to change what information ls  displays.This is possible by adding the command’s optional “flags”.

If you type ls -a, this will display ALL files in a current directory, even hidden files such as dot files.

Typing ls -l will display the list in long-form. Additional information includes date created, date modified, read/write permissions, file size, etc.

Typing ls -t will sort the files in the directory by date last modified, and ls -S will sort the files by file size.

cd .. – Change Directories

cd .. - Change Directories To change directories, you would simply type cd followed by the pathway you are trying to navigate to. For example, to go up one directory from where you are currently are, type cd ..

.. is the universal symbol for going up one directory. For example, if your file path is “Desktop/directory1/directory2”, you would type cd .. . To get back to the Desktop from directory2, you would type cd ../..

The examples above use relative pathways. This is the relationship of the target directory with the current directory. However, you can also navigate using the full path. To navigate to Desktop from any directory, simply type cd Desktop.

mkdir – Make New Directory 

To make a new directory, type mkdir <directoryname> , where <directoryname> is the name of the folder you want to create. This will create a new empty directory wherever you currently are.

rmdir– Remove a Directory 

Similarly, to remove a directory, you would just need to type rmdir <directoryname>.

touch Create a Brand New Empty File

touch is a command you can use to create a brand new empty file, with no content or file type. For example, touch newfile will just create a new empty file entitled newfile.

rm Remove Files

You can remove files by typing rm <filename>.

You can also remove all files in a current directory by adding an asterisk to the command, for example, rm *.

If you would like to remove a folder, including all subdirectories and files inside, you would need to use the recursive option-r , for example rm -r mydirectory.

One needs to be very careful with the rm command, as it is not reversible. It is different from deleting a file and then having it in your trash bin before permanently deleting. Once you remove a file is removed with the rm command, it is totally gone.

cp - Copy a File

To copy a file, you can do so by typing cp <filename> <filename2>. This will copy <filename> to a new file, <filename2>.

You can also use this command to copy a directory. You will need to use the recursive -r flag to also copy all files and subdirectories. For example, cp -r <directory1> <directory2>.

mv - Move a File or Directory

To move a file or directory, you would use the mv command. Typing the command mv <filename> .. would move <filename> up one directory.

You can also use this command to rename a file. Similar to cp, you would type mv <filename> <filename2>. The only difference here is that it just renames the single file, instead of creating a copy.

nano - Text Editor

Nano is a text editor that comes pre-installed with most Unix-like systems. To open up a text file that directly on the command line, simply type nano <filename>.

Nano is a fairly simple text editor, recommended for beginners to the command line interface.Once one is comfortable with Nano, vi or vim (“vi improved”) are far more powerful text editors available for the CLI. Either vi or vim typically come pre-installed on most *nix operating systems. Otherwise be manually installed if they are not.

cat, more, and less

If you would like to display the contents of a file without editing, you can use either cat, more, or less. All of these commands have their advantages and disadvantages. cat typically works well for smaller files. more and less are appropriate for longer files, with the option to display the file one screen at a time. Hitting spacebar will display another screen’s worth of the file.

sudo

sudo is one of the most important commands to know. We’ve waited to explain it, as it is helpful to have some context to understand its purpose.

Every *nix operating system is setup for multiple users, each with different levels of permissions.Permissions specify what a user can and cannot view, edit or remove. If any user of a given system has complete permissions to do anything, this can potentially wreak havoc — modifying or deleting important files could cause irreversible damage, only rectified by wiping the hard drive (or server) clean, and starting from scratch.

The purpose of the sudo command is to temporarily invoke root user (also known as the super user) permissions, allowing full permissions for any file on the system.

Attempting to view a protected file or directory with just a regular user account will pull up the common “Permission denied” message. The only way to access a file like this is to temporarily switch to the root user.

sudo protection

As an example, say you are trying to open a file named “protected-file.txt” that is set to only have view permissions for the root user (for example opening the file in the vim text editor), typing vim protected-file.txt will simply pull up the Permission denied message.

The only way to get around this (aside from changing the user permissions of the file via the chown command), is by prepending the sudo command before the rest of the command you initially tried:

sudo vim protected-file.txt

After entering that command, you will be prompted to enter the password for the root user. Hit enter, and you’ll finally be able to view the file!

There is a small window of time (typically about 1 minute) where you can continue to enter commands requiring root user permissions, without having to re-enter the sudo command. After that times out, you will have to use the sudo command again.

Aside from accessing files you may otherwise not have access to, this can be seen as an extremely useful security measure. Remaining in a user account other than the root user should be a common practice (even for advanced users), as it helps prevent the accidental execution of a command you may have not really intended for. Although it is possible to set your user type as the root user by default, this is rarely recommended to help prevent any serious accidents from having.

Sometimes it’s easy to forget to add sudo before a command that requires it. To avoid having to re-enter the entire command again with sudo prepended to it, you can simply type sudo !! after receiving the Permission denied. This will execute the previous command you entered, but in root user mode.

Pwd command

Pwd command is ideal to use when you have to find the correct path of your existing working folder or directory. The command returns the user to a full or absolute path and highlights all the path directories with (/) a forward slash. A common example of a full path would be /home/username.

Locate command

Locate command This command is useful to locate a specific file like the Windows search command. You can also use locate command with –i argument for case-insensitive files. It is an effective way to search for a specific file even when you forget about its specific name. Whether it’s lowercase or uppercase, you can use the asterisk (*) to search for a specific file that has two or more relational words.
find command
Just like locate command, you can use the find command to search for specific directories and files. But unlike the locate command – you can leverage the find command to find out the location of files within a specific directory. For instance, the find/home/ -title notes.txt command will look for a file name notes.txt in the home directory and as well as its subdirectories.

grep command

Grep is another essential Linux command that users love to use. It allows you to search the entire text file. For example, the grep black notepad.txt command will start to find the word black inside the notepad.txt file. After initiation of the command, users can see the searched word between different lines on the display.

du command

Du command allows users to check the total space each file takes up on a directory. Disk usage or du command creates a disk summary in the form of disk block numbers rather than size format. With the use of the –h argument into the command line, users can also see the occupied disk space in KBs and MBs.

df command

Df command creates a report on your system’s used disk space in percentage. The report shows the used disk space in KBs but you can use df –m argument to view the report data in MBs.

diff command

Difference or diff command makes a comparative analysis of content inside two files. Once the command analyzes the contents of the files, it highlights the lines that are not the right match. You can use the diff command to make changes in programs rather than rewrite the source code.

head command

The head command is useful to check out the first lines of a text file. The default command prompt will show you the ten first lines but users have the freedom to decrease this number. You can also use the–n argument with the head command to see the lines in a text file.

tail command

As the title suggests, the tail command highlights the last rather than the first 10 lines in a text file. You can use the –n argument with the tail command to find the lines inside a text file.

chmode command

Another popular Linux command is chmode that users can use to alter the executable, read, and write permissions of directories and files. In most cases, advanced users take advantage of this command to execute, read, and write permissions accurately.
tar command
The tar is another common Linux command to archive various files into a tarball. It is a famous file format in Linux to zip files and comes with optional compression options. Just like chmode, it is also a complex command and comes with many functions. You can use this command to include new files into a current archive, extract the archived content, and list the archived content.

kill command

Kill command is one of the last responses for users when a program becomes unresponsive. You can use the kill command to terminate the program manually. The command sends a particular signal to the program and initiates the self-termination protocol. SIGTERM and SIGKILL are the two most used signals but there are 64 signals in total.
jobs command
The jobs command makes all existing jobs and their statuses visible. Technically, a job command is a process that boils down to the start of the shell.

wget command

It is another useful and unique Linux command that makes it easier to download any type of file from the internet. Once you type the wget command, you will get the download link menu.

ping command

Ping command is used to check the server status and its connectivity. Once you enter the command ping google.com, the system will check if you can connect to Google and measures the overall response time.

history command

Long-time Linux users know that you can execute hundreds of different commands each day. The history command is useful when you want to take a closer look at all your previously entered commands.

top command

The top command lists all the running processes and CPU usage. It’s the equivalent of Window’s Task Manager. You can use this command to track used system resources and spot processes that are taking up a lot of resources and need termination.

man command

There are certain commands in Linux that still befuddle users. But you can use this command from Linux’s shell to learn more use cases. For example, you can type man tail to see the manual instruction of that tail command.

hostname command

The hostname command is useful to check the name of your network or host. You can also type –i along with hostname to check the IP address of a network.

echo command

The echo command allows users to move around data into a single file. For instance, you can type echo before the new text into filename.txt.
zip and unzip commands
The use of zip command is common t compress files into an archived zip. Conversely, users can initiate unzip command in order to extract the archived zipped files.

uname command

This command represents the shortcut for Unix title and prints out comprehensive information about the installed Linux system. It highlights information like the operating system, the machine name, and the kernel.

angelo frisina sunlight media

Author Bio

Angelo Frisina is a highly experienced author and digital marketing expert with over two decades of experience in the field. He specializes in web design, app development, SEO, and blockchain technologies.

Angelo’s extensive knowledge of these areas has led to the creation of several numerous award winning websites and mobile applications, as well as the implementation of effective digital marketing strategies for a wide range of clients.

Angelo is also a respected consultant, sharing his insights and expertise through various podcasts and online digital marketing resources.

With a passion for staying up-to-date with the latest trends and developments in the digital world, Angelo is a valuable asset to any organization looking to stay ahead in the digital landscape.

3 Comments

  • Joco Wa February 6, 2020 at 5:27 pm

    Please add how to exit from places where you are stuck!

  • Accomplishify August 8, 2021 at 7:21 am

    I am very happy to see this post because it really a nice post. Thanks

  • soundos October 17, 2021 at 8:03 am

    Your blog is very informative. thanks for Sharing this blog.