[Linux] Basic commands

View OS Information

cat /etc/os-release

Or

hostnamectl

Print kernel version:

uname -r

1. Change owner & group of file and folder

chown userA:groupA tmpfile

Copy owner/group setting from a specific file:

chown --reference=fileA tmpfile

Change owner/group of all files in folder:

chown -R userA:groupA folderB/

List all the changes made by chown:

chown -v -R userA:groupA folderB

Change group only if a file already belongs to a certain group:

chown --from=:groupA :groupB tmpfile

2. View folders/files details information

ls -l folderA/
ls -lt folderA/  #also view symlink of folder/file
stat fileA

Check disk space:

df [options] [devices]
df --o #print all available fields
df --output=field1,field2,...
df -h #Human format
df -m #Show output size in one-megabyte
df -k #Show output size in one-kilobyte blocks (default)

Check file size or directory size:

du /etc/ #find out /etc/ directory space usage
du -s /etc/ #total disk space occupied by a directory 
du -sm /etc/ #total disk space occupied by a directory in MB
du -csh /dir1/ /dir2/  #view grand total of multiple directories
du -ah -d 1 --time /dir1/ | sort -h  #view size of all folders/files with sub-tree level is 1, sorting by size

3. Compression and decompression of folder or file:

In linux, we use .tar to archive many files into a single file and use .gz (gzip) to reduce size of large file. Usually, we can use 2 extensions together to make it more effective: use .tar to group files into single file, then compress that file to a smaller size by using .gz.

Extract files from .tar extension:

tar -xf ~/backup-archive.tar

Group multiple files into .tar extension:

tar -c ~/backup > backup-archive.tar

or

tar -cvf backup_archive.tar ~/backup

Compress file to .gz extension. The compression algorithm that gzip uses to compress files can be configured to use a higher amount of compression and thus save space at the expense of time. This ratio is controlled by a numeric argument between -1 and -9. The default configuration is -6. There are several methods to write a command:

gzip full-text.txt gzip -v full-text.txt
gzip --best -v full-text.txt
gzip --fast -v full-text.txt
gzip -3 -v full-text.txt
gzip -8 -v full-text.txt

Decompress .gz extension:

gunzip full-text.txt.gz
gzip -d full-text.txt.gz

An useful way to archive files and compress at the same time:

tar -czf ~/backup-archive.tar.gz ~/backup/

To extract:

tar -xzvf ~/backup-archive.tar.gz

For .zip format:

To compress:

zip squash.zip file1
zip -r9 squash.zip dir1

To decompress:

unzip squash.zip

View content of compressed file without extracting:

tar -tvf file.tar
tar -ztvf file.tar.gz
tar -jtvf file.tar.bz2

tar -tvf my-data.tar.gz 'search-pattern'

4. Executing file as another user

sudo -u userA command-to-execute

5. Users

List all users:

cat /etc/passwd
less /etc/passwd #alternative command
getent passwd #alternative command

List only username:

cut -d: -f1 /etc/passwd
awk -F: '{ print $1}' /etc/passwd #alternative command

Find user:

getent passwd | grep jack

6. Symbolic Links

Hard link:

ln /path/to/file /path/to/hardlink

Symbolic link:

ln -s /path/to/file /path/to/link
ln -s -i /path/to/file /path/to/link  #receive a message asking whether you want to overwrite a file if it already exists

Check if a file is symbolic link:

ls -lt /path/to/fileA

7. Find folder/file

find /dir/path/look/up -type d -name "dir-name-here"
find /dir/path/look/up -name "*.html"

>>to be continued…