Sometimes you want to delete all files in a folder, except for a few that match a pattern. Or you want to move them to a different folder. Using ls, awk and xargs, this can easily be accomplished in Linux or any *nix environment.
Let's say you have thousands of files in a folder and you wish to move all the files that are not from Oct2908 or Jul2707 into a folder called 'old'. Here's the one-liner that accomplishes this with ease:
ls | awk '!/Oct2908*/&&!/Jul2707*/' | xargs -i -t mv ./{} old/{}
First we call ls.
Then we pipe its output to awk, which matches all files that do not contain the pattern 'Oct2908*' and do not contain the pattern 'July 2707*'.
In turn, we pipe this to xargs, which calls mv on each file, moving it to the 'old' folder from the current folder.
Easy enough, and can be changed to any operation instead of mv, and any folder instead of 'old'.
General blog about stuff with a slight bent towards the more interesting things in life: to be bored is to stare into the crypt!
Showing posts with label one liner. Show all posts
Showing posts with label one liner. Show all posts
Monday, December 13, 2010
Thursday, October 28, 2010
Tar and gzip a folder in one line
Sometimes you don't have a fancy GUI to right-click and zip up a folder. All you have is the humble shell. We're talking about Linux or Solaris or AIX or any other Unix-based OS here.
The one-liner to tar and gzip your folder in one shot is
tar cvf - /home/yossarian | gzip - > yossarian.tar.gz
But what does it mean? Simply put, tar the folder called "/home/yossarian" and feed the resulting tar from the stdout of the tar command into the stdin of the gzip command to compress it, then further redirect the output of the gzip command into a filled "yossarian.tar.gz".
It's that simple. Hope this helps you when you need to compress entire folders in the terminal sometime.
Yossarian
The one-liner to tar and gzip your folder in one shot is
tar cvf - /home/yossarian | gzip - > yossarian.tar.gz
But what does it mean? Simply put, tar the folder called "/home/yossarian" and feed the resulting tar from the stdout of the tar command into the stdin of the gzip command to compress it, then further redirect the output of the gzip command into a filled "yossarian.tar.gz".
It's that simple. Hope this helps you when you need to compress entire folders in the terminal sometime.
Yossarian
Labels:
backup entire folder,
command line utility,
gzip,
linux,
one liner,
pipe,
redirect IO,
shell,
stdin,
stdout,
tar,
ubuntu,
unix
Subscribe to:
Posts (Atom)