bash 使用 sed 和 mv 命令取消隐藏 unix 中的隐藏文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20059064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:40:43  来源:igfitidea点击:

unhide hidden files in unix with sed and mv commands

bashunixsedmvhidden-files

提问by Liudis

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

我想知道您是否可以帮助我修复 bash 脚本,该脚本应该取消隐藏目录中的所有隐藏文件。问题出在哪儿?

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

回答by anubhava

This for loopshould work:

for loop应该有效:

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

PS:最好在进行批量 mv/rename 之前备份您的文件

回答by mklement0

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

@anubhava 的答案有效,但这里有一个经过修改的通用解决方案,用于处理隐藏文件/文件夹,其中:

  • 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
    )
    
  • 正确处理边缘情况(没有隐藏文件/文件夹)。
  • 既不依赖也不改变全局状态(配置)。

    ( # 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 GLOBIGNOREhappens to contain .:..):

如果您想避免使用 subshel​​l,您可以使用以下方法,该方法明确排除.以及..未扩展的模式,以防没有匹配项(如果GLOBIGNORE碰巧包含.:..):

    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.

向@jthill、@anubhava、@Mike 致敬。

回答by jthill

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

回答by thom

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)"

测试这些危险游戏的最佳方法是在你的机器上为自己创建一个额外的帐户......如果你搞砸了自己的帐户,将会有“眼泪无限(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