Linux——将文件夹内所有同种类型文件移入到另一个文件夹中

Linux——将文件夹内所有同种类型文件移入到另一个文件夹中

Linux中一切皆文件

摘要

由于工作需要,我需要把一个文件目录A中所有的普通文件移动到另一个目录中,但是问题是,这个文件目录A中有各种类型的文件(目录文件,链接文件等),如何用shell命令实现我的需求呢?本文涉及到awk,ls,$()的使用。

背景及需求

由于工作需要,我需要把一个文件目录A中所有的普通文件移动到另一个目录中,但是问题是,这个文件目录A中有各种类型的文件(目录文件,链接文件等),如何用shell命令实现我的需求呢?

简化需求,如下图,需要将test文件夹中test1,test2,test3文件移入到test4文件夹中,为了方便起见以test命令,实际可能为没有规则的文件名

image-20211014170719381

思路分析

  • 找到test文件夹中所有文件
  • 将其移动到test4文件

如何找到文件夹中所有文件

ls -l +管道符配合grep查看当前目录下某种类型的文件
root@VM-8-17-ubuntu:/tmp/test# ls -l
total 4
-rw-r--r-- 1 root root    0 Oct  2 11:18 test1
-rw-r--r-- 1 root root    0 Oct  2 11:18 test2
-rw-r--r-- 1 root root    0 Oct  2 11:18 test3
drwxr-xr-x 2 root root 4096 Oct  3 11:05 test4
root@VM-8-17-ubuntu:/tmp/test# ls -l |grep ^-
-rw-r--r-- 1 root root    0 Oct  2 11:18 test1
-rw-r--r-- 1 root root    0 Oct  2 11:18 test2
-rw-r--r-- 1 root root    0 Oct  2 11:18 test3
  • 使用ls -l显示文件夹中文件的长格式,第一个字符为文件类型
  • ^表示第一个字符,$表示最后一个字符,可以使用通配符来做筛选
  • 这个方法也可以比较通用,也可以查看当前文件夹中全部目录,全部块文件,全部链接
补充 ls -d的用法
  • ls -d 可以查看当前目录下

  • 直接使用ls -d会查看到当前目录,一般可以使用ls -ld查看当前目录的属性

    root@VM-8-17-ubuntu:/tmp/test# ls -ld
    drwxr-xr-x 3 root root 4096 Oct  3 11:05 .
    root@VM-8-17-ubuntu:/tmp/test# ls -d
    .
    
  • 查看当前目录下所有文件夹的话可以使用ls -d */ ,这种方法文件夹名称后面会带一个/,用法和ls -F很像

    root@VM-8-17-ubuntu:/tmp/test# ls -d */
    test4/
    
补充ls -F的用法

在shell中ls -F是一个很有用的命令:把文件按照类型归类,主要区分目录文件、可执行文件、链接文件,并且在末尾加上 / 、*、@符号标识

  • 对于可执行文件,在后面加上一个*

  • 对于目录文件,在后面加上一个/

  • 对于符号链接文件,在后面加上一个@

  • 对于普通文件,后面没有不会加内容

    可以使用ls -F +管道符配合grep来查看当前目录下某种类型的文件

root@VM-8-17-ubuntu:/tmp/test# ls -l
total 4
-rw-r--r-- 1 root root    0 Oct  2 11:18 test1
-rw-r--r-- 1 root root    0 Oct  2 11:18 test2
-rw-r--r-- 1 root root    0 Oct  2 11:18 test3
drwxr-xr-x 2 root root 4096 Oct  3 11:05 test4
lrwxrwxrwx 1 root root    5 Oct  3 11:22 test5 -> test1
-rwxr-xr-x 1 root root    0 Oct  3 11:18 test5.sh
root@VM-8-17-ubuntu:/tmp/test# ls -F
test1  test2  test3  test4/  test5@  test5.sh*
# 查看当前目录下可执行文件
root@VM-8-17-ubuntu:/tmp/test# ls -F | grep *$
test5.sh*
# 查看当前目录下目录文件
root@VM-8-17-ubuntu:/tmp/test# ls -F | grep /$
test4/
# 查看当前目录下符号链接文件
root@VM-8-17-ubuntu:/tmp/test# ls -F | grep @$
test5@
# 查看当前目录下普通文件
root@VM-8-17-ubuntu:/tmp/test# ls -F | grep -v [/*@]$
test1
test2
test3

如何把找到的文件移动到目标目录内

  1. 使用mv可以将目标文件移动到目标目录中,用法:mv [OPTION]... SOURCE... DIRECTORY

  2. 需要把上一步找到的文件作为SOURCE

使用awk从长格式中得到所需文件的文件名

awk的用法Linux awk 命令 | 菜鸟教程 (runoob.com)

对于2,我们在如何找到文件夹中所有文件中的得到的是长格式的内容,因此可以使用awk命令来找到对应的文件。

root@VM-8-17-ubuntu:/tmp/test# ls -l
total 4
-rw-r--r-- 1 root root    0 Oct  2 11:18 test1
-rw-r--r-- 1 root root    0 Oct  2 11:18 test2
-rw-r--r-- 1 root root    0 Oct  2 11:18 test3
drwxr-xr-x 2 root root 4096 Oct  3 11:48 test4
lrwxrwxrwx 1 root root    5 Oct  3 11:22 test5 -> test1
-rwxr-xr-x 1 root root    0 Oct  3 11:18 test5.sh
root@VM-8-17-ubuntu:/tmp/test# ls -l |grep ^- |awk '{print $9}'
test1
test2
test3
test5.sh
使用$()或者``做命令替换
  • 在bash中,$( )``(反引号)都是用来作命令替换的。
  • 命令替换与变量替换差不多,都是用来重组命令行的,先完成引号里的命令行,然后将其结果替换出来,再重组成新的命令行。
  • 因此,我们可以使用$( )``来做命令替换后得到结果作为mv的SOURCE
root@VM-8-17-ubuntu:/tmp/test# ls -l
total 4
-rw-r--r-- 1 root root    0 Oct  2 11:18 test1
-rw-r--r-- 1 root root    0 Oct  2 11:18 test2
-rw-r--r-- 1 root root    0 Oct  2 11:18 test3
drwxr-xr-x 2 root root 4096 Oct  3 11:48 test4
-rwxr-xr-x 1 root root    0 Oct  3 11:18 test5.sh
# 移动文件至test4文件目录中
root@VM-8-17-ubuntu:/tmp/test# mv `ls -l | grep ^- | awk '{print $9}'` test4
# 查看到之前的文件已经移动到了test目录中
root@VM-8-17-ubuntu:/tmp/test# ls -lR
.:
total 4
drwxr-xr-x 2 root root 4096 Oct  3 12:01 test4

./test4:
total 0
-rw-r--r-- 1 root root 0 Oct  2 11:18 test1
-rw-r--r-- 1 root root 0 Oct  2 11:18 test2
-rw-r--r-- 1 root root 0 Oct  2 11:18 test3
-rwxr-xr-x 1 root root 0 Oct  3 11:18 test5.sh
posted @   zyfzjuer  阅读(2912)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示