Would like to pigz an entire directory in Linux and move each file after it compresses to a new directory

Would like to pigz an entire directory in Linux  and move each file after it compresses to a new directory.

pigz --best -r /directory/backup/ then move .gz file to new directory
LVL 3
Nathan KaufmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve BinkCommented:
[senateuser@MJZFCBM:~/IMAP]
$> ls
total 45
drwxr-xr-x+ 1 senateuser None    0 Apr 14 11:00 ./
drwxr-xr-x+ 1 senateuser None    0 Apr  8 10:54 ../
-rwxr-xr--  1 senateuser None 3076 Mar 18 13:10 ajax_helper.js*
-rwxr-xr--  1 senateuser None 7222 Mar 18 13:10 imap_helper.js*
-rwxr-xr--  1 senateuser None 3222 Mar 18 13:10 imap_reader.css*
-rwxr-xr--  1 senateuser None 6286 Mar 18 13:10 imap_reader.php*
-rwxr-xr--  1 senateuser None 3377 Mar 18 13:10 imap_reader_ajax.php*
drwxr-xr-x+ 1 senateuser None    0 Apr 14 11:00 my_gzips/
-rwxr-xr--  1 senateuser None  503 Mar 18 13:10 raw-text.png*

[senateuser@MJZFCBM:~/IMAP]
$> ls my_gzips
total 8
drwxr-xr-x+ 1 senateuser None 0 Apr 14 11:00 ./
drwxr-xr-x+ 1 senateuser None 0 Apr 14 11:00 ../

[senateuser@MJZFCBM:~/IMAP]
$> for x in $(ls | awk '{print $NF}'); do if [[ -f $x ]]; then gzip -k $x; mv $x.gz ./my_gzips; fi; done;

[senateuser@MJZFCBM:~/IMAP]
$> ls
total 45
drwxr-xr-x+ 1 senateuser None    0 Apr 14 11:01 ./
drwxr-xr-x+ 1 senateuser None    0 Apr  8 10:54 ../
-rwxr-xr--  1 senateuser None 3076 Mar 18 13:10 ajax_helper.js*
-rwxr-xr--  1 senateuser None 7222 Mar 18 13:10 imap_helper.js*
-rwxr-xr--  1 senateuser None 3222 Mar 18 13:10 imap_reader.css*
-rwxr-xr--  1 senateuser None 6286 Mar 18 13:10 imap_reader.php*
-rwxr-xr--  1 senateuser None 3377 Mar 18 13:10 imap_reader_ajax.php*
drwxr-xr-x+ 1 senateuser None    0 Apr 14 11:01 my_gzips/
-rwxr-xr--  1 senateuser None  503 Mar 18 13:10 raw-text.png*

[senateuser@MJZFCBM:~/IMAP]
$> ls my_gzips/
total 29
drwxr-xr-x+ 1 senateuser None    0 Apr 14 11:01 ./
drwxr-xr-x+ 1 senateuser None    0 Apr 14 11:01 ../
-rwxr-xr--  1 senateuser None 1359 Mar 18 13:10 ajax_helper.js.gz*
-rwxr-xr--  1 senateuser None 2181 Mar 18 13:10 imap_helper.js.gz*
-rwxr-xr--  1 senateuser None 1075 Mar 18 13:10 imap_reader.css.gz*
-rwxr-xr--  1 senateuser None 1678 Mar 18 13:10 imap_reader.php.gz*
-rwxr-xr--  1 senateuser None 1410 Mar 18 13:10 imap_reader_ajax.php.gz*
-rwxr-xr--  1 senateuser None  534 Mar 18 13:10 raw-text.png.gz*

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT started powershellCommented:
Reason for adding comment:
1)
This will be closed after 14 days once inactive.
$ /bin/date --date=2016-04-14 "+%d/%b/%Y"
14/Apr/2016

Open in new window

2) For knowledge transfer:
a)
Always use full path while executing any commands.
/bin/ls
/bin/awk or /bin/gawk
/usr/bin/gzip
/bin/mv
b) handle exceptions
if test ! -f my_gzips/currentfile
then
         echo /bin/mv -i currentfile my_gzips/currentfile
         /bin/mv -i currentfile my_gzips/currentfile
fi
murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT started powershellCommented:
1. Helped on wait list (2016-04-14)
2. It was resolved.
3. Knowledge transfer
4. Question inactive more than a year.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.