Command line examples for Linux
commandlinefu.com is the place to record those command-line gems that you return to again and again.
-
Clear Cached Memory on Ubuntu based distributions, and also display memory status. Please do not use this on a production machine unless you really really know what you are doing. Echo 3 is a kinder way of purging the memory, you can also use 'echo 2' or 'echo 1' if #1 You know what you are doing, and #2 refer to number 1 :-)
sudo free && sync && sudo echo 3 | sudo tee /proc/sys/vm/drop_caches
-
Check a directory of PNG files for errors
Useful for checking if a large number of PNG files was downloaded successfully by verifying the built-in CRC checksum. For incomplete files, the command will print: "00002309.png EOF while reading IDAT data ERROR: 00002309.png" The process is very fast; checking 21,000 files of 5MB in size took only five minutes on a 2011 Intel mobile dual-core.
ls *.png |parallel --nice 19 --bar --will-cite "pngcheck -q {}"
-
bash -c 'exec 3<>/dev/tcp/google.com/80; echo EOF>&3; cat<&3'
-
generate pem cert from host with ssl port
openssl s_client -connect HOSTNAME.at:443 -showcerts </dev/null 2>/dev/null | openssl x509 -outform PEM > meinzertifikat.pem
-
Get all lines that start with a dot or period
Read a file or standard input and only outputs lines that start with a dot or period "." Show Sample Output
grep '^\.' file
-
export IP="$(hostname -I | awk '{print $1}')"
-
Download mp3 files linked in a RSS podcast feed
Directly download all mp3 files of the desired podcast
curl http://radiofrance-podcast.net/podcast09/rss_14726.xml | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*mp3" | sort -u | xargs wget
-
This command will take the output of a command and color any STDERR output as a different color (red outline in this case) Show Sample Output
./errorscript.sh 2> >(echo "\e[0;41m$(cat)\e[0m")
-
Combine multiple images into a video using ffmpeg
The -start_number can be ignored if sequence starts with 0, otherwise use first number in sequence
ffmpeg -start_number 0053 -r 1/5 -i IMG_%04d.JPG -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4
-
premiumize - create a ddl & save the URL in variable MYLINK
MYLINK=$(curl 'https://www.premiumize.me/api/transfer/directdl?apikey=dzx3rqwrxme8iazu' \ -H 'accept: application/json' \ -H 'Content-Type: application/x-www-form-urlencoded' --data-raw 'src='$URL | jq -r '.content[] | .link' )
-
Offcloud - add a link as remote download
You will get an json respond if all is fine Show Sample Output
curl 'https://offcloud.com/api/remote?key=XXXXXX' \ -H 'accept: application/json' \ -H 'Content-Type: application/x-www-form-urlencoded' --data-raw "url=$MYLINK&remoteOptionId=XXXXX"
-
checks size of directory & delete it if its to small
only simple example how to combine rclone & jq Show Sample Output
for i in *; do test -d "$i" && ( rclone size "$i" --json -L 2> /dev/null | jq --arg path "$i" 'if .bytes < 57462360 then ( { p: $path , b: .bytes}) else "none" end' | grep -v none | jq -r '.p' | parallel -j3 rclone purge "{}" -v -P ); done
-
Get a range on line with sed (first two)
Get the two first lines of a file and quit. Show Sample Output
sed -n '1,2p;3q' file
-
Remove all the characters before last space per line including it
echo 'The quick brown fox jumps over the lazy dog' | sed 's|.* ||'
-
Get line number 12 (or n) from a file
Extracts only file number 12 from file. It's meant for text files. Replace 12 with the number you want. First line starts at 1 not 0. We use q on next line so doesn't process any line more.
sed -n '12p;13q' file
-
Replace all forward slashes with backward slashes
Use -i option to edit directly a file: sed -i 's|\/|\\|g' file Show Sample Output
echo '/usr/bin/' | sed 's|\/|\\|g'
-
Replace all backward slashes with forward slashes
Use -i to edit file directly: sed -i 's|\\|\/|g' file Show Sample Output
echo 'C:\Windows\' | sed 's|\\|\/|g'
-
Remove all the characters after last space per line including it
It his example removes ' dog', last space included. Show Sample Output
echo 'The quick brown fox jumps over the lazy dog' | sed 's|\(.*\) .*|\1|'
-
Download screenshot or frame from YouTube video at certain timestamp
Downloads the frame of given YouTube video at 8 minutes 14 seconds. Requested format is "299", which 1080p only video.
ffmpeg -ss 8:14 -i $(youtube-dl -f 299 --get-url URL) -vframes 1 -q:v 2 out.jpg
-
grep --color -E 'pattern|$' file
-
Print lines in a text file with numbers in first column higher or equal than a value
A text file contains thousands of numbers. This command prints lines were the number is greater or equal than a specified value (134000000). Show Sample Output
awk '$NF >= 134000000 {print $0}' single-column-numbers.txt
-
Calculate the mean or average of a single column of numbers in a text file
single-column-numbers.txt is a text file with 22658 rows (numbers) in a single column. Each number can range from 0 to 134298679.533591 and the dot is for the decimals. This is done with perl because awk can't sum such high numbers. Show Sample Output
perl -lane '$total += $F[0]; END{print $total/$.}' single-column-numbers.txt
-
find examples of multiline idioms in Linux drivers source code
The Linux Cross Referencer is nice: https://elixir.bootlin.com/linux/latest/source but when you want to find an example usage of an idiom across multiple lines, piping the ack-grep output to less is quicker. The --cc limits the search to C code, -i to ignore case, -A4 because most wait_event variants have 4 arguments that may be on lines after the function name. Use --make to search makefiles Show Sample Output
ack --cc -i -A4 wait_event linux/drivers | less -i # / list_empty
-
seconds since epoch to ISO timestamp
No need to use perl, awk, nor /usr/bin/date -- bash's "printf" builtin will do it. Show Sample Output
printf '%(%FT%T)T\n' 1606752450