Skip to content

Linux Commands#

Basic Linux Commands#

Install .deb files#

1
sudo dpkg -i package_file.deb

Remove an application#

1
sudo apt remove application-name

Create a new file#

1
touch fileName.md

Search a file that you don't remember exactly the name#

  • Search files that contains "school" and "note".
1
locate -i school*note

Search text in a given file#

  • Search all lines in file notepad.txt that contains word "blue"
1
grep blue notepad.txt

View first lines of any text file.#

  • Show the first five lines.
1
head -n 5 filename.md

View the last 10 lines of any text file.#

  • Show the last 10 lines.
1
tail -n 10 filename.md

Compare 2 files line by line#

  • Show lines that are different between 2 files.
1
diff file1.md file2.md

Turn Off Swap Memory#

1
sudo swapoff -a

Open Folder UI From Terminal#

  • Use the command below to open the current folder UI from terminal
1
xdg-open .
  • If you want to open a specific folder UI from terminal then you can put the path.
1
xdg-open xdg-open ~/Downloads

Install .rpm files#

sudo rpm -i package_file.rpm

Fix Can't Mount Drive NTFS#

sudo ntfsfix <drive lable>

# Example

sudo ntfsfix /dev/sda

Mount Drive By Terminal#

  • Create a folder for mounting
1
mkdir ~/share
  • List Disks
1
lsblk
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS                                                     2367* sudo umount /dev/sda                                                            
loop0    7:0    0     4K  1 loop /var/lib/snapd/snap/bare/5                                      2368  cd                                                                              
loop1    7:1    0  63.9M  1 loop /var/lib/snapd/snap/core20/2105                                 2369  clear                                                                           
loop2    7:2    0 349.7M  1 loop /var/lib/snapd/snap/gnome-3-38-2004/143                         2370* sudo umount /dev/sda                                                            
loop3    7:3    0  91.7M  1 loop /var/lib/snapd/snap/gtk-common-themes/1535                      2371* ls                                                                              
loop4    7:4    0  40.9M  1 loop /var/lib/snapd/snap/snapd/20290                                 2372* sudo mount /dev/sda  /home/duc/Shared                                           
loop5    7:5    0 417.2M  1 loop /var/lib/snapd/snap/wine-platform-7-stable-core20/6             2373* systemctl deamon-reload                                                         
loop6    7:6    0 495.1M  1 loop /var/lib/snapd/snap/wine-platform-runtime-core20/91             2374* systemctl daemon-reload                                                         
sda      8:0    0 465.8G  0 disk                                                                 2375  history                                                                         
sdb      8:16   0 238.5G  0 disk                                                                 2376* sudo systemctl daemon-reload                                                    
├─sdb1   8:17   0   600M  0 part /boot/efi                                                       2377* clear                                                                           
├─sdb2   8:18   0     1G  0 part /boot                                                           2378* ls                                                                              
└─sdb3   8:19   0 236.9G  0 part /home       
  • Print device attributes.
1
sudo blkid
  • Mount disk to folder.
1
sudo mount <disk> <mount folder>
1
sudo mount /dev/sda /home/duc/share
  • Mount with Options.
1
sudo mount -o rw,user,uid=1000,dmask=007,fmask=117  /dev/sda  /home/duc/Shared

Auto mount Drives#

  • Find the fdisk list command to see all drives and their partitions.
1
sudo fdisk -l
  • The blkid command allows you to display information about available block devices. This will allow you to view the filesystem type, UUID, labels, and more.
1
sudo blkid
  • Edit fstab file
1
cd /etc/
  • create a copy of fstab file for backup.
1
sudo cp fstab fstab.backup
  • Edit our fstab file. Add line with information like below for the drive that you want to mount it automatically.
1
UUID=a213ed98-641f-4f67-9480-d92a8d7e82f7       /media/duc/Backup   ext4    defaults        0       0
Field Value Description
UUID a213ed98-641f-4f67-9480-d92a8d7e82f7 Universally Unique Identifier for the partition. This ensures a persistent identifier for the filesystem, even if device names like /dev/sda1 change.
Mount Point /media/duc/Backup Directory where the filesystem will be mounted. The partition will be accessible at this path after mounting.
Filesystem Type ext4 Filesystem type for the partition. In this case, it’s an ext4 filesystem, which is commonly used in Linux.
Mount Options defaults Set of default mount options, which includes: rw (read/write), suid, dev, exec, auto (automatically mount at boot), nouser (only root can mount), async.
Dump Field 0 Indicates whether the filesystem should be backed up using the dump utility. 0 means it will not be included in backups.
Pass Field 0 Controls the order of filesystem checking by fsck during boot. 0 means it will not be checked. 1 means check first (usually for root), 2 means check after root.

Tar Command Examples#

options#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
-c  create
-x  extract
-v  verbose mode
-f  filename
-t  view content of archive file.
-j  bzip2
-z  gzip
-r  add
-W  verify
--wildcards

create#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# create tar
tar -cvf FILE_TO_BE_CREATED.tar /path/to/files

# create tar.gz
tar -cvfz FILE_TO_BE_CREATED.tar.gz /path/to/files
tar -cvfz FILE_TO_BE_CREATED.tgz /path/to/files

# create tar.bz2
tar -cvfj FILE_TO_BE_CREATED.tar.bz2 /path/to/files
tar -cvfj FILE_TO_BE_CREATED.tar.tbz /path/to/files
tar -cvfj FILE_TO_BE_CREATED.tar.tb2 /path/to/files

untar#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# untar files in current directory ##
tar -xvf FILE_TO_BE_UNTARRED.tar
tar -xvf FILE_TO_BE_UNTARRED.tar.gz
tar -xvf FILE_TO_BE_UNTARRED.tar.bz2

# untar files in specified Directory
tar -xvf FILE_TO_BE_UNTARRED.tar -C /path/to/files
tar -xvf FILE_TO_BE_UNTARRED.tar.gz -C /path/to/files
tar -xvf FILE_TO_BE_UNTARRED.tar.bz2 -C /path/to/files

# untar single file
tar -xvf FILE_TO_BE_UNTARRED.tar SINGLE_FILE_WITH_ANY_FILE_EXTENSION
tar -xvfz FILE_TO_BE_UNTARRED.tar.gz SINGLE_FILE_WITH_ANY_FILE_EXTENSION
tar -xvfj FILE_TO_BE_UNTARRED.tar.bz2 SINGLE_FILE_WITH_ANY_FILE_EXTENSION

# untar single file, using verbose arguments
tar --extract --file=FILE_TO_BE_UNTARRED.tar SINGLE_FILE_WITH_ANY_FILE_EXTENSION
tar --extract --file=FILE_TO_BE_UNTARRED.bz2 SINGLE_FILE_WITH_ANY_FILE_EXTENSION
tar --extract --file=FILE_TO_BE_UNTARRED.tar.gz SINGLE_FILE_WITH_ANY_FILE_EXTENSION

# untar multiple files from tar, tar.gz, tar.bz2
tar -xvf FILE_TO_BE_UNTARRED.tar "FILE_TWO" "FILE_TWO"
tar -xvfz FILE_TO_BE_UNTARRED.tar.gz "FILE_TWO" "FILE_TWO"
tar -xvfj FILE_TO_BE_UNTARRED.tar.bz2 "FILE_TWO" "FILE_TWO"

# untar group of file using wildcard
# example of wildcard: *
tar -xvf FILE_TO_BE_UNTARRED.tar --wildcards 'WILDCARD_TO_BE_USED'
tar -xvfz FILE_TO_BE_UNTARRED.tar --wildcards 'WILDCARD_TO_BE_USED'
tar -xvfj FILE_TO_BE_UNTARRED.tar --wildcards 'WILDCARD_TO_BE_USED'

list#

1
2
3
4
# list content of tar archive file
tar -tvf FILE_TO_BE_UNTARRED.tar
tar -tvf FILE_TO_BE_UNTARRED.tar.gz
tar -tvf FILE_TO_BE_UNTARRED.tar.bz2

add#

1
2
3
tar -rvf FILE_TO_BE_UNTARRED.tar FILE_TO_BE_ADDED_WITH_ANY_EXTENSION
tar -rvf FILE_TO_BE_UNTARRED.tar.gz FILE_TO_BE_ADDED_WITH_ANY_EXTENSION
tar -rvf FILE_TO_BE_UNTARRED.tar.bz2 FILE_TO_BE_ADDED_WITH_ANY_EXTENSION

verify#

1
2
3
tar -tvfW FILE_TO_BE_UNTARRED.tar
tar -tvfW FILE_TO_BE_UNTARRED.tar.gz
tar -tvfW FILE_TO_BE_UNTARRED.tar.bz2