Find if the file is compressed or not
up vote
0
down vote
favorite
I need to find whether the file is compressed or not in script.If it is compressed I need to uncompress
and send as attachement
. My find command results in two files sum12.pdf.Z
and sum23.pdf.Z
My script is
dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" abc@gmail.com
when I ran this script I got error like
${file: -1}: 0403-011 The specified substitution is not valid for this command.
I am using ksh
.
shell-script find mailx
add a comment |
up vote
0
down vote
favorite
I need to find whether the file is compressed or not in script.If it is compressed I need to uncompress
and send as attachement
. My find command results in two files sum12.pdf.Z
and sum23.pdf.Z
My script is
dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" abc@gmail.com
when I ran this script I got error like
${file: -1}: 0403-011 The specified substitution is not valid for this command.
I am using ksh
.
shell-script find mailx
7
Why don't you use thefile
command to check if it is compressed instead? For examplefile -ib <filename.Z>
will give you the MIME type of the file which you can then use in your tests -applicationx-compress; charset=binary
. You could then check for that exact string or grep forcompress
maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.
– garethTheRed
Jun 25 '14 at 8:34
1
The${file:offset}
syntax was introduced in ksh93, you probably have ksh88. Usecase $file in *.Z) ...
(and don't forget to quote your variables!) (and don't use command substitution on the output of find, use-exec
instead)
– Stéphane Chazelas
Jun 25 '14 at 8:37
@garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
– Aravind
Jun 25 '14 at 15:51
From theman
page forfile
: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?
– garethTheRed
Jun 25 '14 at 16:52
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to find whether the file is compressed or not in script.If it is compressed I need to uncompress
and send as attachement
. My find command results in two files sum12.pdf.Z
and sum23.pdf.Z
My script is
dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" abc@gmail.com
when I ran this script I got error like
${file: -1}: 0403-011 The specified substitution is not valid for this command.
I am using ksh
.
shell-script find mailx
I need to find whether the file is compressed or not in script.If it is compressed I need to uncompress
and send as attachement
. My find command results in two files sum12.pdf.Z
and sum23.pdf.Z
My script is
dir=/home/as1234/bills
cd $dir
for file in `find . -ctime -1 -type f -name "Sum*pdf*"`
do
if [ ${file: -1} == "Z" ]; then
echo "$file is Zipped"
uncompress $file
uuencode $file
fi
done
uuencode $file $file | mailx -s "subject" abc@gmail.com
when I ran this script I got error like
${file: -1}: 0403-011 The specified substitution is not valid for this command.
I am using ksh
.
shell-script find mailx
shell-script find mailx
edited yesterday
Rui F Ribeiro
38.2k1475123
38.2k1475123
asked Jun 25 '14 at 8:02
Aravind
52071432
52071432
7
Why don't you use thefile
command to check if it is compressed instead? For examplefile -ib <filename.Z>
will give you the MIME type of the file which you can then use in your tests -applicationx-compress; charset=binary
. You could then check for that exact string or grep forcompress
maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.
– garethTheRed
Jun 25 '14 at 8:34
1
The${file:offset}
syntax was introduced in ksh93, you probably have ksh88. Usecase $file in *.Z) ...
(and don't forget to quote your variables!) (and don't use command substitution on the output of find, use-exec
instead)
– Stéphane Chazelas
Jun 25 '14 at 8:37
@garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
– Aravind
Jun 25 '14 at 15:51
From theman
page forfile
: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?
– garethTheRed
Jun 25 '14 at 16:52
add a comment |
7
Why don't you use thefile
command to check if it is compressed instead? For examplefile -ib <filename.Z>
will give you the MIME type of the file which you can then use in your tests -applicationx-compress; charset=binary
. You could then check for that exact string or grep forcompress
maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.
– garethTheRed
Jun 25 '14 at 8:34
1
The${file:offset}
syntax was introduced in ksh93, you probably have ksh88. Usecase $file in *.Z) ...
(and don't forget to quote your variables!) (and don't use command substitution on the output of find, use-exec
instead)
– Stéphane Chazelas
Jun 25 '14 at 8:37
@garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
– Aravind
Jun 25 '14 at 15:51
From theman
page forfile
: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?
– garethTheRed
Jun 25 '14 at 16:52
7
7
Why don't you use the
file
command to check if it is compressed instead? For example file -ib <filename.Z>
will give you the MIME type of the file which you can then use in your tests - applicationx-compress; charset=binary
. You could then check for that exact string or grep for compress
maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.– garethTheRed
Jun 25 '14 at 8:34
Why don't you use the
file
command to check if it is compressed instead? For example file -ib <filename.Z>
will give you the MIME type of the file which you can then use in your tests - applicationx-compress; charset=binary
. You could then check for that exact string or grep for compress
maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.– garethTheRed
Jun 25 '14 at 8:34
1
1
The
${file:offset}
syntax was introduced in ksh93, you probably have ksh88. Use case $file in *.Z) ...
(and don't forget to quote your variables!) (and don't use command substitution on the output of find, use -exec
instead)– Stéphane Chazelas
Jun 25 '14 at 8:37
The
${file:offset}
syntax was introduced in ksh93, you probably have ksh88. Use case $file in *.Z) ...
(and don't forget to quote your variables!) (and don't use command substitution on the output of find, use -exec
instead)– Stéphane Chazelas
Jun 25 '14 at 8:37
@garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
– Aravind
Jun 25 '14 at 15:51
@garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
– Aravind
Jun 25 '14 at 15:51
From the
man
page for file
: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?– garethTheRed
Jun 25 '14 at 16:52
From the
man
page for file
: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?– garethTheRed
Jun 25 '14 at 16:52
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
The filename suffix after the final dot may be had with ${file##*.}
.
In this case though, I would consider doing the uncompressing and the uuencoding with find -exec
directly like this:
#!/bin/sh
dir=/home/as1234/bills
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
if [ "${pathname##*.}" = "Z" ]; then
uncompress -c "$pathname"
elif [ "${pathname##*.}" = "gz" ]; then
gzip -cd "$pathname"
else
cat "$pathname"
fi |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
This way, you would support pathnames with spaces and other troublesome characters. The sh -c
script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.
I also added handling of gzip
-compressed files.
Related:
- Understanding the -exec option of `find`
- Why is looping over find's output bad practice?
Alternative implementation of the sh -c
script using case ... esac
instead of multiple if
and elif
statements.
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
case $pathname in
*.Z) uncompress -c "$pathname" ;;
*.gz) gzip -cd "$pathname" ;;
*) cat "$pathname"
esac |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The filename suffix after the final dot may be had with ${file##*.}
.
In this case though, I would consider doing the uncompressing and the uuencoding with find -exec
directly like this:
#!/bin/sh
dir=/home/as1234/bills
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
if [ "${pathname##*.}" = "Z" ]; then
uncompress -c "$pathname"
elif [ "${pathname##*.}" = "gz" ]; then
gzip -cd "$pathname"
else
cat "$pathname"
fi |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
This way, you would support pathnames with spaces and other troublesome characters. The sh -c
script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.
I also added handling of gzip
-compressed files.
Related:
- Understanding the -exec option of `find`
- Why is looping over find's output bad practice?
Alternative implementation of the sh -c
script using case ... esac
instead of multiple if
and elif
statements.
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
case $pathname in
*.Z) uncompress -c "$pathname" ;;
*.gz) gzip -cd "$pathname" ;;
*) cat "$pathname"
esac |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
add a comment |
up vote
0
down vote
The filename suffix after the final dot may be had with ${file##*.}
.
In this case though, I would consider doing the uncompressing and the uuencoding with find -exec
directly like this:
#!/bin/sh
dir=/home/as1234/bills
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
if [ "${pathname##*.}" = "Z" ]; then
uncompress -c "$pathname"
elif [ "${pathname##*.}" = "gz" ]; then
gzip -cd "$pathname"
else
cat "$pathname"
fi |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
This way, you would support pathnames with spaces and other troublesome characters. The sh -c
script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.
I also added handling of gzip
-compressed files.
Related:
- Understanding the -exec option of `find`
- Why is looping over find's output bad practice?
Alternative implementation of the sh -c
script using case ... esac
instead of multiple if
and elif
statements.
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
case $pathname in
*.Z) uncompress -c "$pathname" ;;
*.gz) gzip -cd "$pathname" ;;
*) cat "$pathname"
esac |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
add a comment |
up vote
0
down vote
up vote
0
down vote
The filename suffix after the final dot may be had with ${file##*.}
.
In this case though, I would consider doing the uncompressing and the uuencoding with find -exec
directly like this:
#!/bin/sh
dir=/home/as1234/bills
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
if [ "${pathname##*.}" = "Z" ]; then
uncompress -c "$pathname"
elif [ "${pathname##*.}" = "gz" ]; then
gzip -cd "$pathname"
else
cat "$pathname"
fi |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
This way, you would support pathnames with spaces and other troublesome characters. The sh -c
script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.
I also added handling of gzip
-compressed files.
Related:
- Understanding the -exec option of `find`
- Why is looping over find's output bad practice?
Alternative implementation of the sh -c
script using case ... esac
instead of multiple if
and elif
statements.
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
case $pathname in
*.Z) uncompress -c "$pathname" ;;
*.gz) gzip -cd "$pathname" ;;
*) cat "$pathname"
esac |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
The filename suffix after the final dot may be had with ${file##*.}
.
In this case though, I would consider doing the uncompressing and the uuencoding with find -exec
directly like this:
#!/bin/sh
dir=/home/as1234/bills
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
if [ "${pathname##*.}" = "Z" ]; then
uncompress -c "$pathname"
elif [ "${pathname##*.}" = "gz" ]; then
gzip -cd "$pathname"
else
cat "$pathname"
fi |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
This way, you would support pathnames with spaces and other troublesome characters. The sh -c
script also does not store the uncompressed files but uncompresses them, uuencodes them and sends them in one go.
I also added handling of gzip
-compressed files.
Related:
- Understanding the -exec option of `find`
- Why is looping over find's output bad practice?
Alternative implementation of the sh -c
script using case ... esac
instead of multiple if
and elif
statements.
find "$dir" -type f -ctime -1 -name "Sum*.pdf*" -exec sh -c '
for pathname do
filename=$( basename "${pathname%.pdf*}.pdf" )
case $pathname in
*.Z) uncompress -c "$pathname" ;;
*.gz) gzip -cd "$pathname" ;;
*) cat "$pathname"
esac |
uuencode "$filename" |
mailx -s "subject ($filename)" abc@gmail.com
done' sh {} +
edited Oct 10 at 6:40
answered Jun 22 at 12:10
Kusalananda
116k15218351
116k15218351
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f139068%2ffind-if-the-file-is-compressed-or-not%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
7
Why don't you use the
file
command to check if it is compressed instead? For examplefile -ib <filename.Z>
will give you the MIME type of the file which you can then use in your tests -applicationx-compress; charset=binary
. You could then check for that exact string or grep forcompress
maybe? File extensions aren't used in the Unix/Linux world as they are in Windows.– garethTheRed
Jun 25 '14 at 8:34
1
The
${file:offset}
syntax was introduced in ksh93, you probably have ksh88. Usecase $file in *.Z) ...
(and don't forget to quote your variables!) (and don't use command substitution on the output of find, use-exec
instead)– Stéphane Chazelas
Jun 25 '14 at 8:37
@garethTheRed - in my machine case is not installed. Is there any way to find .Z file?
– Aravind
Jun 25 '14 at 15:51
From the
man
page forfile
: There has been a file command in every UNIX since at least Research Version 4 (man page dated November, 1973). What is your machine?– garethTheRed
Jun 25 '14 at 16:52