# Essential Linux Commands To Get You Started

These essential Linux commands form the basis of your everyday work. It just makes sense to leverage these utilities as much as possible. The whole advantage you could say with Linux is the power and freedom it gives you. With its software repositories and command line environment you can make your computer behave the exact way that you want. You do not have to rely on any other company to allow your computer to run right. This is the importance of Linux and it is why we learn the commands and the way it works.

Linux utilities are the heart and soul of the operating system. They do more than anything in a graphical environment can. For any utility you can see all of its options by using the man command with any utility. So just open a terminal and try them out. Feel free to experiment too.

## Essential Linux Commands and Utilities

This is a starting list of essential Linux commands and utilities that I think are the most useful. Consider this as a reference with examples you can refer to. I use this myself whenever I forget something. There are a lot of commands so is almost impossible to remember them all and how to use them. I hope this article helps and keeps your work moving!

Dnf help

then

then

then

then

## Developing in C++

This is useful when you want to develop in C++ from your workstation.

Install ---- Dnf -y install gcc-c++

then

Build ---- g++your_source.cpp -o your_binary

then

Build example ----g++ test.cpp -o test

then

then

Running example ----./test

then

Manual ---- man g++

## Changing Desktops

Dnf grouplist [to look at desktops available]

Then you use one of the desktops available for your distribution.

Dnf groupinstall "Mate Desktop"

or
Dnf groupinstall "Xfce Desktop"

For example.

## Swap

(can't use swapfile on btrfs)

Swapon -s [to see your current swap space]

Fdisk -l/dev/sda1 [to check that the partition is marked for swap]

then

Fdisk -t/dev/sda1 [alter the partition to make it swappable. You will lose any data on this partition if you do this]

Mkswap/dev/sda1 [prepare the partition]
Swapon/dev/sda1 [activates this swap space]

Dd if=/dev/zeroof=/swapfile bs=1024 count=1048576 [This will create a swapfile of 1gb, specify in kb. For modern systems use swap size double your memory]

Mkswap/swapfile [prepares the swap file just like you would a partition]

Swapon/swapfile [mount it ]

dd this line to your /etc/fstab file using a text editor. /swapfile none swap sw 0 0

Swapoff/dev/sda1 [turns off your swap partition]

In your /etc/fstabfile comment out the line that has swap sw in it. Use a # in front to comment out

Chmod 600/swapfile [You should set your swap file permission to 600]

## Stratis

dnf install stratis-cli stratisd [install]

Stratis pool createmypool /dev/vdg [create your pool]

then

Stratis fs createmypool myfs1 [create filesystem]

then

Stratis fs snapshotmypool myfs1 myfs1-experiment [create filesystem as snapshot of an existing system]

Umount myfs1 [unmount the myfs1 pool]

Stratis fs destroymypool myfs1 [destroy themyfs1 pool]

then

Stratis poollist [listing pools on the system]

then

Stratis fs listmypool [listing filesystems within a pool]

then

Stratis blockdevlist mypool [list the blockdevs that make up a pool]

## Dnf-Automatic

dnf install-y dnf-automatic

Then

Now you need to edit the /etc/dnf/automatic.conf file to do what you want.

Generally I change the following fields from their default values to this.

Then

emit_via = email

Then

email_from = [email protected]

then

email_to = [email protected]

then

email_host = your.smtp.server

Optionally, edit the timer frequency. I leave it at the default of 1 day.
nano /usr/lib/systemd/system/dnf-automatic-install.timer

Then Enable the timer.
systemctl enable dnf-automatic-install.timer

Then you can list the time to see the status
systemctl list-timers --all

## Rsync

Rsync -avzP /source-directory /destination-directory                                                                                                                                                              [copies entire directory to another directory]

This is one of the very essential Linux commands as it is used for backups and in bash scripts a lot.

Rsync -avzP /source-directory-on-local-machine     'ssh -p 2012' [email protected]:/home/username/Documents/                                    [copies all files from local machine to Documents directory of remote server]

## Crontab

Crontab -e [edit crontab entries]

Then

Crontab -l [List or manage the task with crontab command with the -l option for current user]Crontab -u jason -l [list scheduled cron jobs for user jason

Then

Crontab -r [remove crontab entry]

Then

Crontab -I -r [prompt before deleting a crontab]

## Cd

cd      [will take you to the home of the current user you are logged in as]

Or

cd /Music      [will take you to the Music directory]

This is a great essential Linux command. It has a lot of versatility. More importantly, though, it helps you move around in the file system. Here I did [ls] to start with so you could see where I started. Then I decided I wanted to go to my [Documents] directory. All I do then is enter the path there separated by slashes. Then I wanted to go up one folder. The way to do that is [cd ..]. That will take you up 1 directory level. I then used [ls] to see where I was. Lastly I wanted to take a look at my [Music] directory to see if I had uploaded any music yet. I had not alas.

## Clear

• clear       [clears the screen completely]

This is a simple but useful utility. It lets you clear your current window of activity. Sometimes you might have a need to only show certain things in your terminal. This will reset you quickly. I used it several times when writing this post because I did not want my terminal to be cluttered.

There are so many commands in Linux that sometimes it is hard to keep track of what you have done. This is not a huge issue but it comes up from time to time. If you have had a very long terminal session going then you will have a screen full of commands and output from those commands. So occasionally you might just want to clean things up. That is what this command is for.

## History

• history       [this will show you the history of successful commands]

## Man

This stands for manual. It is easy to use and is also the help system for Linux. It not only helps you but will give you more information that you could imagine. Any time you question how to use any utility or feature just use it with the [man] utility. For this reason its one of the essential Linux commands.

man grep     [shows all the information on the grep command]

Then

man mkdir     [shows all the information on the mkdir command]

## Ls

ls     [shows files in current directory]

Or

ls /Music    [shows files in the music directory]

Or

ls -i /Music      [shows detailed files in the Music directory]

This is one of the basic essential Linux commands because it displays information about files. It will work on a single file or multiples if used as an argument. There are a variety of ways to using it. It is one of the basic Linux utilities that everyone uses. Check what directory your in or what files and sub directories are available. Ls has a couple options, -a and -d . The -a option lists everything in the directory. The -d option displays the sub directories.

My favorite option is the -R. It outputs hierarchies of everything there. This is especially useful. The last option I wanted to mention is the -s one. It stands for size. It is always useful knowing the size of a file you are working with for a variety of reasons. Of course you can also combine these options using 2 or more. The files in white are data and the files in blue are directories.

## Cat

• cat error_log     [shows the contents of a file]

This utility displays a file and its contents. It is the simplest of the utilities. Its arguments are any files you want it to join or view. There are options available depending on your needs. One of the most useful is numbering the lines.

## Less

• less songs.txt       [this command lets you view a large file one page at a time]

## Mount

• mount /dev/usb /mnt/usb        [this will make the file system of the usb device available in the folder /mnt/usb]

This command will allow you to see the contents of the usb device that is plugged into your system. Its one of the essential Linux commands because we often have to work with devices and this is one of the ways to do it.

## Pwd

This describes your present working directory. While there is not a lot to this utility, it can still be helpful.

• pwd       [shows you the current working directory]

## Date

date     [shows current date]

Then

date 03081114     [sets the current date to March 8 11:14 am 2018]

This command displays the current time and date on your system. It will label the current timezone too so you know what you are getting. There are a couple options you can use but the main one is [utc]. It is used like this, date -u.

## Cal

• cal       [shows a mini calendar in the terminal]

This is a handy little Linux utility. It shows the month in day form while highlighting the current day. Not much more to be said for this but when your in a hurry and do not want to close window or switch to another one this can give you the information quickly that you need. It is just a 3 letter command  so it is quick.

## Sudo

• sudo nano       [gives yourself superuser privileges to run the nano editor]

This is a very useful and quite essential Linux commands. It allows one to have superuser privileges on a case by case basis. This is important because you will generally have to think about what you are doing while using this. If you run as root all of the time then you will forget the ramifications of your actions at some point and make some drastic mistakes.

## Rm

rm error_log     [removes a file]

Then

rm -r acdc     [removes this directory and all subdirectories]

Then

rm -f error_log     [removes a file forcefully]

This command is what is used to remove files. You should be especially careful of this and double check. As always do not be in root to work with your system unless you have to. A single command done wrongly can wipe out multitudes of files. There are options with this Linux utility also. The -f option will force the files to be deleted even if you do not have sufficient privileges. Please be careful if you ever use it. The -r option will recursively delete directories and everything in them. This is another of the essential Linux commands you will want to use wisely.

## Mkdir

This is also one of those essential Linux commands to know. It is used to make directories and has easy to remember syntax which will help. Mkdir is pretty self explanatory and has a bunch of options. So look at the manual page for them if you are curious or have a need.

Then

mkdir kiss acdc pantera soundtracks     [makes different directories at the same time]

## Cp

cp document1 document2     [copies a file from one location to another]

Then

cp -r folder1 folder2     [copies a directory from one location to another]

This is the copy command. It's one of the essential Linux commands that you will use often. It will copy files or directories to whatever location you designate. There are many options available. For a full list just use [man cp] in a terminal. I will go over the ones I think are most important. The -a option is for archiving and tries to preserve permissions. The -r option is again for recursively copying directory contents over to another directory.

As you can see I did a [ls] command to show the current contents. Then I used [cp] with the -ra options to make a new file with a new name. Then I did another [ls] command to show that it did work.

## File

This is a quite useful utility. When working in a command line environment it is sometimes harder to know what a certain file is. This command will give you details of the file argument without having to resort to using a gui. It gives important details of whatever it is. The arguments will be the file you want information of. As you can see it says whether it is text or a directory. I also showed that you can have multiple options per line. It gives a nice output in the order that you asked.

## Grep

grep "jason" error_log     [searches for any mention of the word "jason" in the file error_log]

Then

grep -i "jason" error_log     [same search but ignores the case of "jason"]

Then

grep "jason" *     [searches for any mention of "jason" in any file under current directory]

Then

grep -r "jason"     [searches for any mention of "jason" in any file in the computer]

This is one of the most important essential Linux commands and is the foundational search command. It is the most useful one in my opinion so it pays to know it well in how to use it. It can search in 1 or multiple files at once. You use it to look for strings of text that will identify something you are looking for. Its first argument is the pattern you are looking for. The you direct it where to look for that pattern at. There are various options for using this.

The -c option lists all the files it finds a match in and then displays how many matches are in that file. This is useful for relevancy. The -color option will output any matches in color on your screen to make it easier to see what has been found. The -w option will look for any word that you use as an argument. This is also very useful.

## Tar

This becomes one of the essential Linux commands for you when you need to extract or pack files. Many times when you download something off the internet it will be in a version like this. Just use this utility to unpack it.

## Df

Monitoring your system is very important and should be done often. That is why this is an essential Linux command.

• df -h     [this shows your disk space]

This is a very useful Linux utility. It will give you the amount of free disk space on your system. Quite useful for monitoring how fast your hard drive is filling up. it gives some very nice information. It is always a good idea to watch this.

## Reboot

This little command shuts everything down and will restart your system. It is certainly quicker than using a mouse to go through a menu.

## Lpr

This is also one of  essential Linux commands and is how you print from the command line. It works by sending the file to be printed to the print queue. It is done this way so other users on the system can print as well. The queue then just prints in the order it gets the requests from. The arguments are the path names or just a file to be printed if it is from the current directory. Various options are available. The -r option will remove a file to be printed. The -#n option will print however many copies that you designate.

## Mv

Moving and renaming files is a very important task. That is why this is one of essential Linux commands.

• mv file1 file2     [renames file1 to file2]

The move command does what it says. It will move a file to another location. Be careful as this can delete files and you will lose their contents if done wrong. The arguments of this Linux utility are the path and filenames to be moved and location to where they are to moved to. The most useful option here is the -i option because it will ask you to confirm when overwriting a file.

## Sort

The sort utility is very useful when looking at a list of directories or files. You can use it to sort and merge files. Its arguments are the file or files to sorted or merged. There are options, of course, but if used without any options then it will sort alphabetically. To merge, just use the 2 things to merge as arguments and their contents will be merged and sorted. The -n option will sort in numeric order if you have a list of numbers somewhere.

Another useful option is the -o option with an argument of a filename to be created and the information stored into. It will not send to your screen then but to a file that you designate. The -r option will sort in reverse. It works with either non-numeric or numeric characters. This is a very useful command if you have any data that needs to be sorted. It is a very useful Linux utility.

## Tail

tail -f error_log     [shows you the last portion of a file]

Then

tail -30 error_log     [shows you the last 30 lines of the error_log file]

The tail Linux utility will output the last 10 lines of a file to your screen. This is useful when you need to quickly know the contents of a configuration file. The best option it has is for adjusting how many lines you want it to count. The default is 10 but if you use -8 and then your filename then it will output the last 8 lines of your file. In this example I was curious about what this log file was about.

If you have ever opened up a big log file before then you will know what I mean. It can take forever to get to the end or even the middle of the file. That is where this essential Linux command comes in handy. As you can see I just asked for the last 20 lines. It saved me a bunch of time. It is truly one of the essential Linux commands.

## Wc

This utility I wanted to mention is the word count command. It is a great Linux command for it is very useful at outputting information about a file. It does more than just words of course as it can count lines and size as well. This is all very interesting too. Without any options it will put on screen the number of lines, words, and bytes in a file. Its arguments are the file or files to be analyzed.

It can do multiple at once and then analyze both at once. Its options are ways of controlling its output. The -l option will only show the number of lines in a file. The -m option will show only the bytes. Lastly, the -w option will show only the number of words in that particular file. As you can see I analyzed [alpha] and [small_file]. It then gave me a total between the 2 files.

## Ps

ps     [this command shows us the process id's that are running for the current user]

Then

ps -al     [shows us the process id's that are running for all users]

The Linux file system has many processes running at once. It is these processes that allow you to do things useful on your computer. You can see i do not have a lot running right now. Most of you will be using more at once than I am now. Pay attention to that [PID] number. That is how you identify any process. Its one of the essential Linux commands because you will often have to look at what processes are running on your system.

## Kill

• kill -9 1012       [this command will kill the process ID 1012]

This is a very useful Linux utility. It goes well with the above mentioned [Ps] command. If you ever get a process that is not responding or that you just need to get rid of then use the [Kill (PID number)]. As you can see above everything has a PID number. If something is not working correctly then you can use this command to end it.

## Chmod

Another of the essential Linux commands, changing permissions ona  file is very important.

chmod +x song     [allows any user to execute this file]

Then

chmod +r song     [gives read permission to all users]

Then

chmod 755 song.txt       [gives you full permissions and everyone else just read/execute]

Then

chmod 777 song.txt       [gives everyone full permissions on the file]

This is how you change permissions. Linux has a very flexible permission system. It is good to understand what is happening even if you are just a user. This command deserves its own article soon. That is how in depth and valuable it is. I will get an updated version up soon.

## Chown

chown jmoore songs.txt       [this will change the owner of the file songs.txt to the user jmoore]

Then

This goes right with [Chmod] as it is used to change the ownership of a file.

## Du

du     [shows disk space in current directory]

Then

du -h     [shows disk space in smaller increments]

Then

du -h pantera     [shows disk space used by the directory pantera]

This is handy because it will show you what is using your disk space. It can give information about either files or directories. You can also choose whether you want detailed information or not.

## Top

top     [shows process id's and resource utilization]

Then

top -u root     [shows process id's and resource utilization from the root user]

This is another of the most essential Linux commands and it gives you a lot of good information about your system. It is especially handy if you are trying to figure out why your system seems sluggish.

## Uname

uname     [gives you the operating system you are using. not entirely helpful as you probably knew you were running Linux]

Then

uname -a     [gives you the operating system and version you are using. more helpful]

## Gzip

• gzip zipfile     [compresses files to save storage or bandwidth]

Compressing files will be be very important. That is why this is one of the essential Linux commands.

## Gunzip

• gunzip file1     [extracts the files after they have been previously compressed]

This is a good utility to have. You will see many files that have been compressed. Use this to see the information.

## Sftp

This is another of our essential Linux commands that uses the sftp protocol for transferring files over a network. It is secure by nature and much better to use than the older ftp protocol.

## More

• more error_log     [shows the contents but allows you to go through the contents slowly]

This is very useful if you have a large file. It will allow you to navigate through the file slowly.

## Scp

• scp file.html [email protected]:/path to folder       [this will upload a file to a folder on your server.]

If you have a website, for example, this is one of those essential Linux commands that will let you put a new page or some customizations up to your server.

## Passwd

• passwd       [this is how you change your password. you will then be prompted twice to enter in the new one]

## Exit

• exit       [this command will end your shell session that you are in]

## Logout

• logout       [this will log you out of the computer you are on]

## Fdisk

Working with disks is a common occurance. So Fdisk is also one of those essential Linux commands you will want to be familiar with.

At [Command :] use the [p] option to get the overview of the hard disk at any time.

Then
At [Command:] use the [d] to delete existing partitions.

Then
At [Command:] use the [F] to list free space.

Then
At Command: use the [n] option to create a new partition.

Then
Partition type p is for primary and e is for extended. Use default for sectors unless you have reason to change them.

Then
Use [w] option to write changes to each device.

If you are going to use raid on Linux then MDADM is one of the essential Linux commands you will want to work with.

fdisk -l
df -h
mdadm -D /dev/md0 [check status of raid device]

Then
mdadm -r /dev/md0 /dev/sdb2 [remove selected raid device on selected disk]

Then

Then
mdadm /dev/md0 --stop [stops the raid device]

Then
mdadm /dev/md0 --assemble [starts the raid device again]
Lsblk
umount -l /dev/mdX [to dismantle a raid array]

Then

## KVM

dnf install @virtualization
systemctl start libvirtd

Then

systemctl enable libvirtd
lsmod | grep kvm [To verify that kvm properly loaded. You are looking for either kvm_intel or kvm_amd]

## DD

Stands for data duplicator
Used for copying and converting data
dd if=<source file name> of=<target file name> [Options]
If stands for inputfile and of stands for outputfile
dd if=/dev/sda of=/dev/sdb     [clones one disk to another]
Then
dd if =/dev/sda2 of=~/hdadisk.img     [backs up sda2 as an image file]
Then
dd if=hdadisk.img of=/dev/sdb3     [restores this image file to another machine]
Then
dd if =/dev/sda2 | bzip2 hdadisk.img.bz2     [compress the image file as your taking a backup]
Then
dd if=/home/imran/abc.txt of=/mnt/abc.txt     [used to copy files]
Then
dd if=/dev/zero of=/dev/sdb     [wipes the device sdb]
Then
dd if=/dev/random of=/dev/sdb     [randomly writes data to disk so it cant be recovered]
Then
for i in {1..10};do dd if=/dev/random of=/dev/sdb;done     [shell script to random write 10 times to make sure]
dd if=/dev/zero of=/swapfile bs=1024 count=200000     [create virtual file system to be used as swap]
Then
dd if=/dev/dvd of=/opt/my_linux_image.iso     [create .iso file from dvd]
Then
dd if=/home/\$user/bootimage.img of=/dev/sdc     [creates bootable usb]

## Cockpit

This nice utility lets you manage virtual machines and network devices.

sudo yum install cockpit cockpit-dashboard
Then
sudo systemctl enable cockpit.socket
Then
sudo systemctl start cockpit.socket
Then
sudo systemctl status cockpit
firewall-cmd --add-service=cockpit
or
firewall-cmd --permanent --add-port=9090/tcp
localhost:9090