[英]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?我怎样才能做到这一点?
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.