简体   繁体   English

如何通过 shell 脚本从目录名称中删除特定前缀

[英]How to delete specific prefix from directory name by shell script

I created several directly like following我直接创建了几个,如下所示

admin_users/
admin_cars/
admin_roles/
admin_.../
... other 8 directories.

Now, I want to move these files to admin directory, and delete suffix.现在,我想这些文件移动到 admin 目录,并删除后缀。

users
cars
roles
... other 8 directories.

How can I do this?我怎样才能做到这一点?

With [p]rename (which is available on most systems):使用[p]rename (在大多数系统上都可用):

rename 's/_/\//' admin_*/

admin_*/ expands to directories whose name starts with admin_ , and s/_/\// replaces the first underscore with a slash. admin_*/扩展到名称以admin_开头的目录, s/_/\//用斜杠替换第一个下划线。

So, I take that you want to delete the prefix 'admin'?所以,我认为你想删除前缀“admin”? If you are sure that that's going to be the name of your directory, you can manually create the directory admin.如果您确定这将是您的目录名称,您可以手动创建目录 admin。

After that,在那之后,

  • you can loop through the current directory (provided you run the script from the concerned directory)您可以遍历当前目录(前提是您从相关目录运行脚本)
  • find all the directories找到所有目录
  • extract the part after the underscore提取下划线后的部分
  • use it to rename and move in the admin directory.使用它来重命名并在管理目录中移动。

Something along the lines of this --类似这样的东西——

#!/bin/bash

for file in `ls -l | grep "^d" | awk '{print $9}'`; do
    suff=`echo $file | cut -d '_' -f 2`
    echo $suff
    mv $file admin/$suff
done

Now, I want to copy these files to admin directory, and delete suffix.现在,我想将这些文件复制到 admin 目录,并删除后缀。

You said you want to Copy those directories to another parent dir.您说过要将这些目录复制到另一个父目录。 cp command supports defining the target file name: cp命令支持定义目标文件名:

cp -r admin_users /path/to/admin/users

You can just write a loop for that.您可以为此编写一个循环。 For the "removing prefix" part, say f is the variable in your loop:对于“删除前缀”部分,说f是循环中的变量:

kent$  echo $f
admin_users
kent$  echo "/path/to/admin/${f##admin_}"
/path/to/admin/users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文件中提取特定行并将其附加到Shell脚本中的另一个现有文件中,然后从原始文件中删除? - how to extract specific lines from file and append it to another existing file in shell script and then delete from original? 在特定目录中以其他用户的身份从PHP调用Shell脚本 - Calling shell script as another user from PHP in specific directory 从任何(非特定)目录运行shell脚本 - Running a shell script from any (non-specific) directory Linux, shell 脚本] 如何通过按文件夹名称搜索来动态删除文件夹 - Linux, shell script] How to dynamically delete folders by searching by folder name 如何从特定行启动shell脚本? - How to start a shell script from a specific line? 如何从另一个Shell脚本运行一个Shell脚本,该脚本从CSV文件中读取第一个Shell脚本名称 - How to run a shell script from another shell script reading first shell script name from a CSV file 从脚本更改外壳目录? - change shell directory from a script? 如何从shell命令中删除目录中的每个其他文件? - How to delete every other file in a directory from a shell command? 在Shell脚本中删除以特定字符串开头的目录 - Deleting a directory starting with a specific string in shell script 如何使用 shell 脚本从文件中删除某些单词? - How to delete certain words from files with shell script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM