Essential Unix Commands for Beginners

Last updated on

To secure a data job in 2024, mastering Unix isn’t just a nice-to-have—it’s a necessity. 🎯

I am sure you already know the importance of this versatile operating system, right?

For those on their learning journey, I have a complete list of essential Unix commands for beginners at your fingertips that can make the process significantly smoother.

Instead of searching multiple sources, you can now access a comprehensive, organized command list all in one place, ensuring you have the tools you need exactly when you need them.

If you’re committed to mastering Unix and searching for a quick command reference, your search ends here.

List of Unix Commands

ls command

used to list the files and directories.

ls
ls -l
ls -a
ls -i
ls -R
OptionUsage
-a to display all files including hidden files(beginning with .)
-lLong listing the file i.e. with attributes like permissions, size etc.
-ito list directory contents along with inode number
-Rto list directory contents along with the inode number

pwd command

used to display the present working directory.

pwd

cd command

used to change the directory.

  • cd directory_name (This command takes you directly to the folder you need to work in)
cd home/user/dir1
  • cd .. (This command navigates you back to the previous directory, allowing you to backtrack through your directory structure quickly.)
cd ..

man command

used to view the manual for a specific command

man command_name
//example
man ls

mkdir command

used to create new directories

mkdir directory_name
//example
mkdir dir1

rmdir command

used to remove the directory

rmdir directory_name
//example
rmdir dir1

cal command

used to display calendar

cal (Displaying the current month)
cal -3 (Displaying the previous, current and next month)
cal -y (Displaying whole year)
cal -m (Starting the week from Monday)
cal -ym (Whole year calendar starting from Monday)

Date command

used to display dates

date
date -d 10/07/2024
date +"%a %d %b %y"

who command

used to display who is logged in

who
who -m
-mDisplaying all Information of Logged-In Users
-a Displaying all Information of All Logged-In Users
-qDisplaying Login Names and Terminal Line Numbers
-HDisplaying Output with Headings
-bDisplaying Time of Last System Boot

echo command

used to display a line of text

echo "You are learning unix"
output: You are learning unix

echo -e "This is unix \n 2nd line"
output: this is unix
        2nd line

echo "23+56" | bc
output: 79

cat command

used to concatenate, display or append the files

  • Write into the file
cat>file1
this is first line
this is second line
  • Append the file
cat >> file1
this is third line added
  • View the file
cat file1
  • Concatenate a file
cat file1 file2 > combined_file

Pipelining

with the help of the pipe, the output of the command can be given as input to the next command.

-lto print the line count
-wto print the word counts
-cto print the byte count
-mto print the character count
cat access.log | wc

head command

used to display first ‘n’ lines from the file.

head -n filename
//example
head -2 file1

or,

cat filename | head -n
//example
cat file1 | head -2

tail command

used to display the last ‘n’ lines from the file.

tail -n filename
//example
tail -2 file1

or,

cat filename | tail -n
//example
cat file1 | tail -2

cut command

used to extract columns from each line of the file

-bBytes, select only these bytes.
-cCharacters, select only these characters.
-dDelimiter, select only the basis of the delimiter provided
-fSelect only specified fields
//example1
cut -d '/" -f2 access.log

//example2
cut -d "-" -f1,2 access.log | cut -d ' ' -f 1,3

uniq command

to display the unique values

cut -d ' ' -f1,3 access.log | uniq -c

sort command

used to sort the elements in ascending or descending order

-tto specify the delimiter
-kto specify starting and ending columns for sorting
-rto sort in reverse order
-nto some kind of numerical data
-uto some numerical data
-cto remove the duplicate line
//example1
cut -d ' ' -f1,3 access.log | uniq -c | sort

//example2
cut -d ' ' -f1,3 access.log | uniq -c | sort -r

tr command

translating character

-ddeleting the character
-scompressing multiple consecutive character
-ccomplementing values of expression
//converts loweracase to uppercase
tr '[a-z]' '[A-Z]' < logs.txt
tr -d '[]' < logs.txt

grep command

used for searching and manipulating text patterns within files.

grep 'Inverter' access.log | tail -5
//for case insensitive search add -i
grep -i 'inverter' access.log
grep '^12' access.log
grep '8[1-9]' marks

pattern matching

*to match zero or more occurrences of the previous character
[pqrs]character class- to match a single character p,q,r or s
[^pqrs]to match a single char which is not p,q,r or s
^patternto match the pattern at the beginning of the line
pattern$to match the pattern as a whole line
^$to match lines containing nothing
+match one or more occurrences of previous char
?match zero or one occurrence of the previous char
pattern1|pattern2to match pattern1 or pattern2
(Fin|Eng)landto match Finland or England

cp command

used to copy files and directories

cp [options] source destination
-i or interactivePrompts before overwriting.
-r or -RRecursively copies directories.
-u or updateCopies only when the source file is newer than the destination file or when the destination file is missing.
-v or verboseShows files being copied.
-a or archivesPreserves attributes and copies directories recursively.
-b or backuptake backup of destination
  • copy a file
cp file1.txt file2.txt
  • copying multiple files into directory
cp file1.txt file2.txt file3.txt /path/to/destination/

mv command

to move or rename files. Like cp command, it will moved to the destination location but the source copy is deleted

  • -b or –backup
  • -i or –interactive
  • -u or update
  • -n or -no-clobber
mv file1.txt /backup_dir/file2.txt

Access Permissions

Types of Permissions

  1. Read (r): Allows viewing the contents of the file or directory.
  2. Write (w): Allows modifying the contents of the file or directory.
  3. Execute (x): Allows executing the file or accessing the directory.

Permission Categories

Permissions are assigned to three categories of users:

  1. Owner (u): The user who owns the file or directory.
  2. Group (g): The group that owns the file or directory.
  3. Others (o): All other users.

Viewing the permission

ls -l filename
//output
-rwxr-xr--
rwx
0000
1001–x
2010-w-
3011.wx
4100r–
5101r-x
6110rw-
7111rwx

Changing the permission

to change the permission “chmod” is used

-Rto change the file and directory permissions recursively
–referenceto use the permission of the first argument as a reference to set permission of remaining arguments
chmod -R 755 /home/user1
chmod -R ugo+x /home/user1/app_dir

Working with an editor

In VI editor, there is 3 mode:

  • command mode

When you enter the VI editor default you will be in command mode.

  • insert mode(interpreted as raw text)

To insert text into the file, you need to switch to ‘VI’ insert mode.

you can type text into the file or use arrow keys to navigate

(n)hmove n position left
(n)jmove n lines down
(n)kmove n lines up
(n)lmove n position right
$move to end of current line

Operations with insert mode

iinsert data before cursor
Iinsert data at start of current line
ainsert data after cursor
Ainsert data at end of current line
ostarts a new line after cursor
Ostarts a new line before cursor
  • escape mode(keys are interpreted for saving/exit purpose)

After insert mode, if you press ESC you will enter into command mode. On typing a ‘:’ you can enter into escape mode andgve command to save and quit the editor.

To save and quit the VI editor

:wsave changes
:wqsave changes and exit
:qexit from editor
:q!exit from editor and discard changes made

Closing Words

These essential Unix commands are where you begin your journey with the command line.

Regular practice will boost your confidence in using them effectively.

For detailed Unix commands, you can reference the websites like geeksforgeeks.

I hope you found this helpful!

Feel free to leave any questions or suggestions in the comments below.

Enjoy learning and exploring!✨



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *