Bash One-Liner

Bash One-Liner

Here some quick remember (for me and maybe for you) of some bash one-liner syntax. In fact, I use daily some « for loop » one-liners but I regularly forget how to include an « if statement ».

Bash One-Liner : syntax

For loop one-liner is the idea to use a for loop in a one line context.
In a script or program you will write it on multiple lines as follow :

for i in {1..10}
do
    echo ${i}
done

Pretty annoying for simple action… There we are, for loop (among others) can be written as a one-liner command.

for i in {1..10} ; do echo ${i} ; done

Here some exemple

me@laptop:~$ for i in {1..100} ; do touch File-${i} ; done
me@laptop:~$ for i in $( ls File-* | head -n 50 ) ; do echo "Not empty anymore" >> $i ; done

And What if I want to test some conditions and do something if my statement is true ?

me@laptop:~$ for i in File-* ; do if [ -s ${i} ] ; then echo "is empty !" > ${i}.emptyFlag ; else echo $i "is not empty" ; fi ; done
me@laptop:~$ for i in File-* ; do if [ -e ${i}.emptyFlag ] ; then rm $i ${i}.emptyFlag ; else :  ; fi ; done

Bash One-Liner : example

Here some examples in a more detail and in a reproductive manner. Let’s start a scratch directory :

me@laptop:~$ mkdir sandbox-OneLiner
me@laptop:~$ cd sandbox-OneLiner
me@laptop:~/sandbox-OneLiner$ pwd
/home/me/sandbox-OneLiner

Let’s create a bunch of test file:

me@laptop:~/sandbox-OneLiner$ for i in {1..100} ; do touch File-${i} ; done

Let’s check a bit what we create it :

me@laptop:~/sandbox-OneLiner$ ls File-* | head -n 2
EmptyFile-1
EmptyFile-10

If you want to sort you result numerically, you can pass the list to sort and ask him to use the second column of the string (-k 2). String will be cut using the ‘-‘ character (-t -).

me@laptop:~/sandbox-OneLiner$ ls File-* | sort -t - -k 2 -g | head -n 2
EmptyFile-1
EmptyFile-2

Let’s check how many file do we have :

me@laptop:~/sandbox-OneLiner$ ls File-* | wc -l
100

Now, we will add some content at half of the files. And in a same time put a flag on the empty one (just by creating a new file with an explicit extension). Then we will remove every files who posses the flag.

me@laptop:~/sandbox-OneLiner$ for i in $( ls File-* | head -n 50 ) ; do echo "Not empty anymore" >> $i ; done
me@laptop:~/sandbox-OneLiner$ for i in File-* ; do if [ -s ${i} ] ; then echo "is empty !" > ${i}.emptyFlag ; else echo $i "is not empty" ; fi ; done
me@laptop:~/sandbox-OneLiner$ for i in File-* ; do if [ -e ${i}.emptyFlag ] ; then rm $i ${i}.emptyFlag ; else :  ; fi ; done
me@laptop:~/sandbox-OneLiner$ ls File-* | wc -l
50

Ok there is a quick way to do it (and many more to just remove empty files) :

# starting by removing all the files :
me@laptop:~/sandbox-OneLiner$ rm File-*
me@laptop:~/sandbox-OneLiner$ for i in {1..100} ; do if [[ $i < 51 ]] ; then touch File-${i} ; else echo "Not empty" > File-${i} ; fi ; done
me@laptop:~/sandbox-OneLiner$ for i in File-* ; do if [ -s ${i} ] ; then rm $i ; else : ; fi ; done

For the specific task of deleting empty file there is the find command

me@laptop:~/sandbox-OneLiner$ rm File-*
me@laptop:~/sandbox-OneLiner$ for i in {1..100} ; do if [[ $i < 51 ]] ; then touch File-${i} ; 
me@laptop:~/sandbox-OneLiner$ find . -size 0 -delete

Some useful link :
http://www.bashoneliners.com/

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.