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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM