Linux Boot Traveller
Linux Boot Traveller

Reputation: 49

Can't take file names with spaces in shell script for loop

I am trying to take filenames with spaces Get them into a for loop zip each file Once zipped, remove the original using below:

for appdir in $(ls /home/location/$dirname); do

  for logFile in $(find /home/location/$dirname/$appdir -type f -mtime +10 \( ! -iname "*.zip" ! -iname "*.gz" \)); do
    echo $logFile

Files I am trying to zip which has spaces:

/home/location/hdd1/domain/servername/EXT1/ET SOME To FILENAME/xml/sdlfkd.xml
/home/location/hdd1/domain/servername/EXT1/ET SOME To FILENAME2/xml/sd2fkd.xml

I know filenames with spaces can be made into a single filename like below -

find /location/$appdir -type f -mtime +10  -print0 | xargs -0 ls

The actual zip command which takes the filename from the "for" condition comes later to take each file, makes a zip and removes the original one.

I get below error for zip command:

[stdout: 4]                           zip warning: name not matched: ET
zip error: Nothing to do! (ET.zip)
[stdout: 4]           SOME
[stdout: 4]                           zip warning: name not matched: SOME
zip error: Nothing to do! (SOME.zip)

How can I modify the condition where I can provide entire filename each time to the "for" loop so it then proceeds to zip each file and goes further? Any help would be appreciated.

Upvotes: 3

Views: 448

Answers (1)

Will
Will

Reputation: 2410

Always use double quotes when manipulating files.

See for example the code below will create one unique file called a b c :

will /home/will/a # touch "a b c"
will /home/will/a # ls -l
total 0
-rw-r--r--    1 root     system            0 Sep 21 08:47 a b c

But this will create 3 different files called, a, b and c

will /home/will/a # touch a b c
will /home/will/a # ls -l
total 0
-rw-r--r--    1 root     system            0 Sep 21 08:47 a
-rw-r--r--    1 root     system            0 Sep 21 08:47 b
-rw-r--r--    1 root     system            0 Sep 21 08:47 c

So now imagine I would be not creating but deleting or altering those files, it could be much more harmful if files name were not the ones expected.

In your case I would try this :

   for appdir in $(ls "/home/location/$dirname"); do

  for logFile in $(find "/home/location/$dirname/$appdir" -type f -mtime +10 \( ! -iname "*.zip" ! -iname "*.gz" \)); do
    echo "$logFile"

Upvotes: 1

Related Questions