Mostly Used Linux Commands
Linux | Using Terminal Commands | Bash | Command Line Interface
Let’s start with basic information about Linux CLI and work on mostly used commands in daily life.
Introduction 📌
The Command Line Interface (CLI), is a non-graphical, text-based interface on computer system, where the user types in a command and the computer executes it.
Basic Command Syntax : command [options…] [arguments…]
ls -l Documents
- arguments : An input that command acts on it. ➡️ Documents
- options : A flag or switch that modifies behaviour of command. ➡️ -l
Commands 💻
Displaying current directory
Using pwd command you can display your current working directory.
pwd
Changing Directories
Using cd command you can change your current directory. Initial directory of the user is /home/<username> in Linux. The root directory is / .
cd [PATH]
examples
cd Desktop/
cd ..
cd
In Linux . represents the current directory and .. represents the directory above your current directory. You can go to upper directory using cd .. or you can go to user’s home directory using only cd or cd ~ command.
Listing Files
Using ls command you can list the directories and contents. Also ls command provides more detailed information about contents and directories using -l option.
ls [OPTIONS][FILE]
examples
ls -l
ls -a
ls -l Desktop/
ls -lSr
ls -lr Documents/
You can see that -l option adds additional information to result.
Column 1
drwx-w-r-x
1. symbol : file (-), directory (d), socket (s) etc
2,3,4. symbol : users permissions
5,6,7. symbol : group permissions
8,9,10. symbol: others permissionsColumn 2
emreuysal
shows the user ownerColumn 3
staff
shows the group owner Column 4
288
shows the sizeColumn 5
Nov 28 18:00
shows the last modification time Column 6
Desktop
shows the name
For hidden files and directories you can use -a option and hidden files starts with . in Linux.
To sort result according to size you can use -S option and if you want to reverse the sort you can use -r option.
Administrative Access
Using su and sudo commands you can have administrative access on Linux. The difference between sudo and su is; sudo gives administrative access only for the command to be executed.
su [OPTIONS]
sudo [COMMAND]
examples
sudo cat example.txt
su -l
Changing File Permissions and Ownership
In linux there are 3 ownerships of permissions; users, groups and others. For each ownership there are read, write and execute permissions.
r : read ➡️ 4 | w : write ➡️ 2 | e : execute ➡️ 1
chmod [PERMISSIONS][FILE]
chown [USER][FILE]
examples
chmod 777 example.txt
chown emreuysal example.txt
Using chmod command you can change the file permissions.
Using chown command you can change the file ownership.
Viewing Files
For viewing file content you can use cat, head and tail commands.
cat [FILE]
head [OPTIONS][FILE]
tail [OPTIONS][FILE]
examples
cat example.txt
head -n 5 example.txt
tail example.txt
To read all content of the file cat command is a good choice, but reading some number of the content from the start or end, you can use head or tail command, also -n option with a number allows to increase or decrease the number of the lines to be read.
Copying, Moving and Removing Files
Copying and moving files in Linux requires 2 parameters. First one is the source and the second one is the destination.
Copying a file is provided by cp command and moving a file is provided by mv command.
cp [SOURCE][DESTINATION]
mv [SOURCE][DESTINATION]
rm [OPTIONS][FILE]
examples
cp example.txt Desktop/
mv Desktop/example.txt Documents/
rm -rf .
Lets remove file from Documents using rm command. Normally rm only removes given file but you can delete a directory using -r (recursive) option. Be careful when using this command because it does not send files to trash can, it deletes files permanently.
Filtering
You can filter data using grep command and it supports advanced regex patterns to filter data professionally.
grep [OPTIONS,PATTERNS etc.]
examples
cat sample.txt | grep '[a-z]'
The example above simply shows filtering output of cat command. First grep command shows the output containing 0 and the second one uses regex pattern that contains number between 0–9. By the way, | (pipe) means send output of the command on left hand side to right hand side command.
Shutdown, Password Update and Change Command Name
You can shutdown your computer using shutdown command but this command requires root privilages.
shutdown [TIME][MESSAGE]
examples
shutdown +1 "bye bye"
When you need to update your password or another user password you can use passwd command.
passwd [OPTIONS][USER]
examples
passwd emreuysal
If you want to use shorter commands you can create shorter names for them using alias command.
alias [ARGUMENTS]
examples
alias myname='echo "emre"'
Summary ⨋
In summary, we worked on mostly used Linux commands and tried them. If you want to learn more about Linux and CLI, I strongly recommend registering a Linux Essentials course and follow my articles.
References 📖
I will share 2 magical commands as a reference. Please try and see the outputs.
man [COMMAND]
info [COMMAND]
examples
man ls
info cat