Every Linux user has their favorite one line command. In this article we will share 20 of them with you
(By Ricardo Gerardi, Roberto Nozaki, Anthony Critelli and Jose Nunez)
By the end of this article, you will have:
The shell ‘{}’ operator is great for this
mkdir -p -v \
/home/josevnz/tmp/{dir1,anotherdir,similardir}
You want to replace a string on one or more file, without using an editor? Sure, sed to the rescue:
/bin/sed -i 's#ORIGINAL_VALLUE#NEW_VALUE#g' myfile1 myfile2
But wait, Perl lovers will tell you they can do the same:
/bin/perl -p -i -e 's#ORIGINAL#NEW_VALUE#' myfile1 myfile2
Raise your hand if you haven’t use this at least once:
cd $mydir && python3 -m http.server 8888
If you are in a hurry is a time saver but also you should ever, ever run an untested script like this:
curl --silent --fail --location \
--output - \
http://localhost:8000/myscript.sh \
|/bin/bash -s
So if the script keeps changing and you are low in disk space, this is perfect. Also means you can execute more dynamic generated Bash commands (just by pointing the URL to the right location)
SSH + Tar to make secure backups. It is like peanut butter and jelly:
/bin/tar --create --directory /home/josevnz/tmp/ \
--file - *| \
ssh raspberrypi "tar --directory /home/josevnz \
--verbose --list --file -"
You can spicy it with compression, encryption… Just like a sandwich.
Big fan when I you need to write multiline documents
/bin/cat<<DOC>/my/new/file
Line1
Line2
A $VARIABLE
DOC
The grep way (pretty fast and easy to remember)
grep -R 'import' --include='*.java' \
--color MySourceCodeDir
Or the ‘find’ way (xargs to handle a large number of matches properly)
find MySourceCodeDir/ -name '*.java' \
-type f -print| \
xargs /bin/grep --color 'import
Why find you may ask? You can tell find to execute actions with ‘-exec’ on your files first, then pass it to the filter. Processing possibilites are endless here…
This is almost cheating. Repeate a command every 5 seconds and highlit the differences
/bin/watch -n 5 -d '/bin/free -m'
(Or if you don’t want to check the free column of ‘vmstat 1’).
lsbk (ls block) + jq (to manipulate JSON on the CLI). Never been easier:
josevnz@dmaf5 ~]$ /bin/lsblk --json| \
/bin/jq -c '.blockdevices[]|[.name,.size]'
["loop0","55.5M"]
["loop1","156M"]
["loop2","32.3M"]
["zram0","4G"]
["nvme0n1","476.9G"]
Note: functions are superior and can do the same as an alias:
function wi { test -n "$1" && stat \
--printf "%F\n" $1; }
If you have an RPM based system sooner or later you will format your queries:
rpm --queryformat='%12{SIZE} %{NAME}\n' \
-q java-11-openjdk-headless
Find acts as a filter, then you get the size in bytes of each file and finally you accumulate the total size…
t=0; for n in \
$(find ~/Documents -type f -name '*.py' -print| \
xargs stat --printf "%s "); do ((t+=n)); done; echo $t
Or if you want a function (better)
function size { t=0; test -d "$1" && for n in $(find $1 \
-type f -name '*.py' -print| \
xargs stat --printf "%s "); do ((t+=n)); done; echo $t; }
size $mydir
$ for i in */.git; do cd $(dirname $i); \
git pull; cd ..; done
$ podman run --rm \
-v .:/usr/share/nginx/html:ro,Z \
-p 30080:80 -d nginx
Check the weather (using a function):
$ weather() {curl -s --connect-timeout 3 \
-m 5 http://wttr.in/$1}
A frequent one I use with NGINX (I think it works with Apache also) to grab the top 10 IPs hitting a webserver from the access log:
cat /var/log/nginx/access.log | \
cut -f 1 -d ' ' | \
sort | \
uniq -c | \
sort -hr | \
head -n 10
But you can do pretty cool stuff with Python, nut just limited to rounding numbers:
echo "22.67892" \
| python3 -c "print(f'{round(float(input()))}')"
23
function to define a quick calculator on the command line with variable precision (default 2):
function qqbc() { echo "scale=${2:-2}; $1" | bc -l
Now I can perform a quick calculation like this:
$ qqbc "2/3"
.66
In case you need additional precision just define a second parameter:
$ qqbc "2/3" 4
.6666
This is called qqbc because it’s an improvement on my the old function qbc :wink:
A modification of this popular recipe.
python3 -c \
"import csv,json,sys;print(json.dumps(list(csv.reader(open(sys.argv[1])))))" \
covid19-vaccinations-town-age-grp.csv
Another favorite. If you have Docker installed and you want to run a command without installing a bunch of dependencies on your system (while doing a quick run) then this may be all you need:
docker run --rm --interactive --verbose --location --fail \
--silent --output - \
https://raw.githubusercontent.com/josevnz/tutorials/main/20%20-single-line-commands-you-would-not-be-able-to-live-without-on-Linux.md
This will let you run the latest version of curl from a container, to later remove it. The possibilities are endless here.