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

眉间皱痕 提交于 2021-01-27 12:24:35

问题


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.


回答1:


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"


来源:https://stackoverflow.com/questions/46336884/cant-take-file-names-with-spaces-in-shell-script-for-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!