Skip to content

Instantly share code, notes, and snippets.

@nd3w
Last active April 2, 2024 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nd3w/dfaac200e8a0c1b12ac8fda889f841f2 to your computer and use it in GitHub Desktop.
Save nd3w/dfaac200e8a0c1b12ac8fda889f841f2 to your computer and use it in GitHub Desktop.
A Collection of Useful Linux Commands
Backup with bunzip
tar cvfj file.tar.bz2 *
tar cvfj file.tar.bz2 /path/to/directory_to_backup/
Extract from bunzip to directory 'archive'
tar -xvf archive.tar.bz2 ./
Compress multi directories into each own compressed files
for i in */; do zip -r "${i%/}.zip" "$i"; done
for i in */; do tar -czvf "${i%/}.tar.gz" "$i"; done
for i in */; do 7z a "${i%/}.7z" "$i"; done
Untar multiple tarballs using bash for loop
for f in *.tar.bz2; do tar -xvf "$f"; done
List file and its subdirectories, filename only
ls -1R > lsir.txt
List files sort by last modified date
ls -ltr
List file and its size separate by a tab
ls -ls | awk '{printf $10"\t"$6/1000000"\n"}' > list.txt
Find files after a certain date
find . -type f -newermt '2024-04-01 00:00:00'
Copy a file into a directory and its subdirectories:
sudo find <target-dir> -type d -exec cp <the file> {} \;
Change permission files recursively:
sudo find /var/www/html/ -type f -exec chmod 644 {} +
Change permission directory and its subdirectories recursively:
sudo find /var/www/html/ -type d -exec chmod 775 {} +
Change ownership on directory with a lots of files
sudo find /path/to/dir -type f -exec chown www-data:www-data {} \;
Delete a certain type of files recursively:
sudo find ./ -name '*.php' -exec rm -rf {} \;
or
sudo find . -name "*.php" -delete
Disc usage for top level dirs only:
sudo du -h -d1 /
Total disc usage on a dir:
sudo du -sh /root/test
Total disc usage on a dir in kilobytes (Kb):
sudo du -shk /root/test
Biggest 10 dirs in a given path:
du -a -h /path | sort -h -r | head -n 10
Find files containing string 'happy birth' with matching line
grep "happy cat" /home/tom/*.txt
Find files containing string 'happy birth' with file name only
grep -H "happy cat" /home/tom
Find files containing string 'happy birth' with file name only (case insensitive)
grep -H -i "happy cat" /home/tom
Find text in files in a directory recursively
grep -rH -i "Johnny" DirName/
Find text in files in a directory recursively, list filename only
grep -rHl -i "Johnny" DirName/
Find text in files in a directory recursively, supress error message
grep -rs "Johnny" .
Replace all occurrence of <br> to <br /> in files in a directory
find ./ -type f -exec sed -i 's/<br>/<br \/>/g' {} \;
Find out a file's MIME type
file --mime-type image.png
file -b --mime-type image.png
file -i FILE_NAME
Empty a text file
sudo truncate -s 0 theFile
Remove duplicate line
sort file.txt > file-sorted.txt
uniq file-sorted.txt file-remove-duplicate.txt
Find unique lines in a file and sort them
sort file.txt | uniq | less
Find files less than 200 Kb recursively, omit -delete to test it. (G for giga; M for mega; k for kilo)
find /mnt/ADrive/APath/ -type f -size -200k -delete
Rename - add prefix to all files (from 01.txt to PRE_01.txt)
rename 's/^/PRE_/' *
Rename files and replace dots with spaces (sadly with files' extensions too!)
rename 's/\./ /g' *
And rename back the extension
rename 's/ mp4/\.mp4/g' *
rename 's/ srt/\.srt/g' *
Rename files starts with 'note-', e.g.: note-worthy.txt, to 'nota-'
rename 's/note-/nota-/g' *
Rename all files to capitalize (e.g.: FILENAME FIRST.txt to Filename First.txt
rename 's/.*/lc($&)/e; s/(^| )./uc($&)/ge' *
Add extension html to all files in current directory
find . -type f -print0 | xargs -0 -I{} mv "{}" "{}".html
Remove files when 'rm -f' fails because there are too many files
find . -name "*.pdf" -print0 | xargs -0 rm
find . -name "*.pdf" -print0 | xargs -0 sudo rm
Count how many files in a directory recursively
find ./directory_name -type f | wc -l
Display first 100 lines of a text file
head -100 filename.log
Display last 100 lines of a text file
tail -100 filename.log
Print all chips temperature:
sensors
Print drive(s) temperature:
sudo hddtemp /dev/sda (for drive /dev/sda)
sudo hddtemp /dev/sdb (for drive /dev/sdb)
sudo hddtemp /dev/sd? (for all drive)
Global replace in Vim
:%s/search_string/replacement_string/g
Replace hex 91 and 92 characters in Vi
:%s/[\x91\x92]/'/g
Save file as another file in Vi
:w filename.ext
Search for non ASCII characters
/[^\x00-\x7F]
Display all partitions/drives
sudo lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL
Make a bootable USB flash disc
sudo dd if=/home/ISO/YourDistro.ISO of=/dev/sdx
Where /dev/sdx is your pendrive. Make sure it is unmounted.
Disable/enable touchpad
View ID prop:
# xinput | grep Touchpad
Result:
ETPS/2 Elantech Touchpad id=14 [slave pointer (2)]
To disable:
# xinput disable 14
To enable:
# xinput enable 14
Send POST request using cURL
curl --location --request POST 'https://example.com/api/login.php' \
--header 'Authorization: Bearer Bearer zw4yofevyziuosxucoqb' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "foo",
"password": "secret"
}'
Restart XFCE4 panel
xfce4-panel -r
Convert multiple png image files to jpg
mogrify -format jpg *.png
Resize all files to certain dimension
mogrify -resize 960x528 *.png
Resize all files to certain width and preserved ratio for height
mogrify -resize 960 *.png
Resize all files to certain dimension but will preserved ratio
mogrify -resize 960x528! *.png
Print directories and files consuming the most disk space
du -a -x {/dir1/} | sort -n -r | head -n 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment