Basic Linux Commands You Should Know While Using Ubuntu/Linux

One misconception about Linux is that, it is very hard to use Linux and we should know many Linux commands. But it is very easy to use Linux if you know only few basic Linux commands.

I am going to describe some of important Linux commands here which are heavily used. I have written these commands in sequential way so if you are completely new with Linux, it would be better if you go with one by one.

1. cd

cd is for change directory and used to change current working directory in terminal.

cd MyFolder

2. ls

ls can be used to list all the files folders in the current directory.

Open terminal and type ls and press enter.

ls

You will see all the files and folders in that directory.

You can change working directory to Desktop with command,

cd Desktop

3. sudo

It was superuser do originally. Whenever you need to run some command with admin privileges you need to put sudo before you command.

sudo mkdir MyFolder

4. cat

cat is used to create and view files directly from terminal. To create a file named a.txt enter cat > a.txt and hit enter and then type text again hit enter and type ctrl+z to save the file.

cat < a.txt
hi my name is Sangam.

To view a file,

cat a.txt

5. rm

rm command is for removing a file. To simply remove a file file.txt run

rm a.txt

If you need admin privilege to remove that file then the command become

sudo rm a.txt

To remove a folder,

rm -R folder

Here -R is for recursive removal otherwise a folder will not delete.

6. mkdir & rmdir

mkdir for creating a folder and rmdir for removing a folder

mkdir A
rmdir A

7. cp & mv

cp is for copying files. It takes two arguments, one is for source and next for destination. However it also takes options. You can see my typing cp –help in terminal. To learn about cp and mv commands we will create some folders and files first.

mkdir MainFolder
cd MainFolder
mkdir A B
cd A
cat > a.txt
file from folder A
cd ../
cd B
cat b.txt
file from folder B

Now you have to remember that the working directory is ~/Desktop/MainFolder/B. To go to MainFolder which is one level up type,

cd ../

To copy a file to a new folder. First create a file in MainFolder and then copy to folder A,

cat > myFile.txt
cp myFile.txt A

In above example folder A is in the same directory where myFile.txt is.

With mv command we can move and rename files and folders. To rename myFile.txt to MainFile.txt ,

mv myFile.txt MainFile.txt

To move MainFile.txt to folder B,

mv MainFile.txt B

If there was no folder B then above command will rename MainFile.txt to B. So instead of above command you can try this,

mv MainFile.txt B/

So what if you like to move or copy a folder into another folder. Lets move folder A into B. You can also copy in the same way.

mv A B/

The / after folder B denotes that it should be moved inside B. Otherwise it will rename folder A into B as I said previously.

In similar way can try copying and moving files and folders but be careful and do not alter system files.

8. touch

touch is also for creating files, but only empty file can be created. To create file with touch command,

touch file.txt

To create multiple files with touch command,

touch file1.txt file2.txt

9. clear

clear is to clear the outputs in terminal. All the outputs/results will be wiped from terminal after running this command.

clear

10. chmod

chmod short form of change mode, is used for changing the file permission. For example to make a file executable,

chmod +x app.deb

Similarly to make a file non executable,

chmod -x app.deb

For example to set a file permission in following way,

  • user can read, write and execute
  • members of your group can read and execute
  • other can only read
sudo chmod u=rwx,g=rx,o=r myfile.txt

In the above command u, g and o represents user, group and other respectively. r, w and x represents read, write and execute respectively.

In numeric form the above command is like,

sudo chmod 754 myfile.txt

11. chown

chown is for changing the owner of a file. For example,

sudo chown username myfile.txt

12. man

Displays manual of a command. To see manual of chmod,

man chmod

You can also try.

chmod --help

13. zip, unzip & gunzip

To compress files into zip,

zip myarchive file1.txt file2.txt

To decompress file from a zip archive,

unzip myarchive.zip

For more info run zip –help in terminal

To decompress a .gzip or .gz you need gunzip,

gunzip archive.gz

14. tar

To extract a tarball,

tar xvf archive.tar
tar xvf archive.tar.xz
tar xvf archive.tar.gz

To extract .tar.gz file

tar xvzf archive.tar.gz
tar xvf archive.tar.gz

x – extract, v – verbose output, z – decompress .gz file, f – name of the tarball and must be at last so that name of file must be immediately after it.

Some command line that are really useful

Updating system and apps

sudo apt-get update
sudo apt-get upgrade

Installing packages from repository

sudo apt-get update
sudo apt-get install package-name

Always run apt-get update before upgrading or installing packages from repository.

Installing a .deb (Debian) package

sudo apt install ./package.deb

or

sudo apt install /path/to/package.deb

Fixing broken packages

sudo apt-get install -f

Installing a package that are in tarball (.tar.gz)

Extract the files inside tarball, move it to the desired location (optional step) and run the executable, for example,

tar xvf app.tar.gz
sudo mv app /opt/
sudo /opt/chrome/executable.sh


More articles for you

Receive New Post by Email

Leave a Comment

5 thoughts on “Basic Linux Commands You Should Know While Using Ubuntu/Linux”