Tuesday, November 29, 2022

Learning Linux - Essential Commands(Part-1)

As part of this blog series, I am trying to consolidate the most common Linux commands and their usage in a very simplified manner so that you may recall them while you're performing any related tasks.


- Log into the local/remote system
SSH, Telnet, VNC Server/client

- Read, and use System Documentation
command --Help, Manual Pages with man command, 
Searching for commands - apropos, Tab/auto-completion

- Create, Delete, Copy, and Move Files and Directories
Listing files & Directories - ls, absolute path/relative path,
Current/Working Directory - cd, Creating file - touch,
copying file - cp [source] [destination], Move Files - mv [source] [destination]
Deleting files & Directory - rm,

- Create and manage hard links
Introduction to iNode - stat [file], hard link - ln [path_to_target_file] [Path_to_link_file],
Limitations - Only Hard links to files, not folder, Only Hard links to files on same filesystem,

- Create and manage soft links
soft links - ln -s [path_to_target_file] [Path_to_link_file], readlinks [soft link file]

- List, set, and change standard file permissions
Owner & Groups - chown [user] [file], chgrp [group] [file], Files & Directory Permission - 
chmod [permissions] [file/directory], 

- SUID, SGID, and Sticky Bit
SUID - chmod 4664 file, SGID - chmod 2664 file, Stickybit - chmod 1777 file

- Search for files
find [path/to/direcotory] [search parameters], 
                Search Parameters - Name - find -name felix, 
                Search Parameters - Modified time - find -mmin [minute],  
                Search Parameters - file size - find -size [size], 
                Search expressions - find -name "f*" -mmin [minute] # AND Operator, 
                find -name "f*" -o -mmin [minute] # OR Operator,   
                find -not -name "f*" ,  find /! -name "f*" # NOT Operator, 
                # Find files with permissions- find -perm 664 // find -perm u=rw,g=rw,o=r, 
                # Find files with at least permission- find -perm -664 // find -perm -u=rw,g=rw,o=r, 
                # Find files with any of these permissions- find -perm /664 // find -perm /u=rw,g=rw,o=r

- Compare and manipulate file content
cat, tac, tail, head, Transforming text: sed 's/canda/canada/g' file.txt(lookupmode), 
                Sed -i 's/canda/canada/g' file.txt(inplacemode)
cut -d '' -f 1 file.txt(delmitedmode), Uniq & Sort: uniq file.txt, sort file,
Comparing files: diff diff file1 file2, diff -c diff file1 file2 , diff -y diff file1 file2

- Search files using Grep
Searching with grep: grep [option] 'search pattern' file, 
                grep 'centos' /etc/os-release, 
                grep -i 'centos' /etc/os-release(Non-case-insensetive),
grep -r 'centos' /etc/os-release(recursive), 
                grep -ir 'centos' /etc/os-release(Case-Ins.+recursive),                 
                grep -vi 'centos' /etc/os-release(invert-match), 
                grep -wi 'centos' /etc/os-release(words), 
                grep -oi 'centos' /etc/os-release(only-matching), 

- Analyze text using basic regular expressions
Regex Operators: ^, $, ., *, +, {}, ?, |, [], (), [^], 
                The Line Begin with: ^, 
                The line End with: $, 
                Match AnyONE Characters: . Example: grep -r 'c.t' /etc/,
Escape for special characters: \: Example: grep '\.' \etc\login.defs, 
                Match the previous element o or more matches: *, 

- Extended Regular Expressions
Previous elements can exist "this many" times: {}, Example: egrep -r '10{,3}' /etc/, 
                Make the previous element optional: ?, Example: egrep -r 'disabled?' /etc/, 
                Match one thing or the other: |, Example: egrep -ir 'enabled?|disabled?' /etc/, 
                Range or Sets: [], Example: egrep -r 'c[au]t' /etc/, 

-  Use input-output redirection (>, >>, |, 2>, etc.)
Stdin(<), stdout(1>, >), and stderr(2>), Redirection: >, >> , 
                Example: over right, date > output.txt, 
                append, date >> output.txt 
                heredoc(<<EOF) and here string(<<<) 
                Example: sort <<EOF .Input value. > EOF (Here Document or heredoc), 
                bc <<< 1+3+6 == 10(here string)

- Archive, backup, compress, unpack, and uncompress files
1. Archiving, 2. Compression, 3. Backup
tar = tape archive, 
                Listing: tar --list --file archive.tar, 
                Creating tar file: tar --create --file archive.tar file1, 
                Add to exising archive: tar --append --file archive.tar file1,
Extracting: tar --extract --file archive.tar --directory /tmp/

- Compress and Uncompress files
Common archiving tools: tar, zip 
Common Compress and Uncompress tools: gzip, bzip2, xz
Compress: gzip file1, bzip2 file2 xz file3, 
                Decompression: 
                 gunzip file.gz or gzip --Decompress file.gz, 
                 bunzip file.bz2 or bzip2 --Decompress file.bz2,
unxz file.xz or xz --Decompress file.xz

Common Compress and Uncompress with tar: 
                 tar --create --file archive.tar file1, 
                 tar --create --gzip --file archive.tar.gz file1(with combining gzip),
tar --create --bzip2 --file archive.tar.bz2 file1(with combining bzip2), 
                tar --create --xz --file archive.tar.xz file1(with combining xz)
Autocompress: 
                 tar --create --autocompress --file archive.tar.gz file1(Select compression utility automatically), 
                 tar --extract --file    archive.tar.gz 

- Backup files to a Remote System
Syncing two Directories: 
                rsync, Example: rsync -a pictures/ aroon@9.9.83.2:/home/aroon/pictures/
Disk Imaging: dd, 
                 Example: sudo dd if=/dev/vda of=diskimage.raw bs=1M status=progress

- Securely transfer files between systems
Securely transfer files: 
                 scp, Example: scp aaron@192.168.1.27:/home/aaron/myfule.tgz /home/aaron/myfiles.tgz (Copy from remote to local), 
                 scp /home/aaron/my_archive.tar aaron@192.168.1.27:/home/aaron/my_archive.tar (local to remote), 
                 scp aaron@192.168.2.37:/home/aaron/my_archive.tar aaron@192.168.1.27:/home/aaron/my_archive.tar ( remote to remote)

Securely transfer files: sftp, Example: sftp aaron@192.168.1.27(connecting sftp server), 
                 get family.jpg (interactive command(get) to download to local), 
                 get -r /picture (interactive command(get) to download recursive to local), 
                 put family.jpg (interactive command to upload to local),  
                 put -r /my_pictures (interactive command to upload recursive  to local)




-> echo "Thank You :) "