unhide hidden files in unix with sed and mv commands

邮差的信 提交于 2019-12-10 16:59:03

问题


i wonder if u could help me with fixing bash script which should unhide all hiden files in dir. Where is the problem?

param='.'
for file in $param*; do
mv $file $(echo $file | sed 's/^.\(.*\)/\1/')
done
exit

回答1:


This for loop should work:

export GLOBIGNORE=".:.."
for file in .*; do
   mv -n "$file" "${file#.}"
   # mv -n "$file" "${file:1}"
done

PS: Better to backup your files before doing a mass mv/rename




回答2:


@anubhava's answer works, but here's an amended, generalized solution for processing hidden files/folders, which:

  • correctly deals with edge cases (the absence of hidden files/folders).
  • neither depends on nor alters global state (configuration).

    ( # Execute in subshell to localize configuration changes below.
    GLOBIGNORE=".:.."   # Do not match '.' and '..'.
    shopt -s nullglob   # Expand globbing pattern to empty string, if no matches.
    for f in .*; do     # Enumerate all hidden files/folders, if any.
      # Process "$f" here; e.g.: mv -n "$f" "${f:1}"
    done
    )
    

If you want to avoid a subshell, you can use the following approach, which explicitly rules out . and .. and also an unexpanded pattern in case there are no matches (if GLOBIGNORE happens to contain .:..):

    for f in .*; do
     if [[ $f != '.' && $f != '..'  && -e $f ]]; then
        # Process "$f" here; e.g.: mv -n "$f" "${f:1}"
     fi
    done

Tip of the hat to @jthill, @anubhava, @Mike.




回答3:


I wouldn't mass-rename them like that myself, I'd add visible symbolic links to them:

while read f; do
        ln -s "$f" "visible-${f#./}"
done <<EOD
$(find -mindepth 1 -maxdepth 1 -name '.*')
EOD



回答4:


This will only unhide all hidden files, keep away from your home directory!!!

ls -1Ap |grep "^\." |grep -v "/" |while read F; do mv $F ${F:1}; done

This will unhide all hidden files and dirs, again: keep away from your home directory!!!

ls -1A |grep "^\." |while read F; do mv $F ${F:1}; done

Best you can do to test these kind of dangerous games is to make yourself an extra account on your machine...If you screw up your own account there will be "tears unlimited(tm)"

If you want to test it first (which is a VERY sensible thing to do):

ls -1A |grep "^\." |while read F; do echo "mv $F ${F:1}"; done


来源:https://stackoverflow.com/questions/20059064/unhide-hidden-files-in-unix-with-sed-and-mv-commands

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