For f in * -- too many arguments

Hi guys,

I am trying to make a script that will archive all the folders (except NOARCHIVE folder) and then remove those folders:

cd my_specific_folder;

for f in *; do
if [ -d $f -a $f != "NOARCHIVE" ]; then
        tar czf $f.tar.gz $f && rm -r $f;
fi;
done && echo "All the folders within my_specific_folder are archived.";

There are around 150 folders and I get the following error: Too many arguments. I tried using find . | xargs but unsuccessfully.

Could you please give me some hints?

Try using double quotes..

if [ -d "$f" -a "$f" != "NOARCHIVE" ]; then
tar czf $f".tar.gz" $f && rm -r $f;

Hi,
first fix your code - quote your variables:

cd my_specific_folder;

for f in *; do
if [ -d "$f" -a "$f" != "NOARCHIVE" ]; then
        tar czf "$f".tar.gz "$f" && rm -r "$f";
fi;
done && echo "All the folders within my_specific_folder are archived.";

Then re-run the code and check if that fixes the issue.
If that's not the case, please post the exact command and output/error message that you're getting.

1 Like

Thank you radoulov. It worked now.I assume that some folders have a space within the name.