簡體   English   中英

用於排序文件的bash腳本

[英]bash script for sorting files

嗨,我是 bash 腳本的新手。 我正在嘗試編寫一個代碼,該代碼為我提供用戶在提示時輸入的用戶名的所有文件以及用戶輸入的文件修改日期。 我無法使用用戶提供的日期對文件進行排序和顯示。 請幫忙。

#!/bin/bash
echo "Enter your username: "
read username
echo "Enter the date file is created: "
read date
ls -l | find mypath -user "$username" | find mypath -regextype posix-extended -regex "^[0-9]*[- / .][0-9]*[- / .][0-9]*$" "$date"

一步一步地檢查你的代碼。

ls -l 

將打印正在運行腳本的目錄的內容。 然后您將該輸出發送到find ,這使得運行ls -l變得多余,因為 find 不從標准輸入中獲取任何內容

然后,您將find mypath -user "$username" (現在只是一個文件列表)的輸出發送到另一個 find 命令,該命令再次不從 stdin 獲取任何內容。

另外一個“查找創建日期”的快速谷歌返回了這個線程,它告訴我們你甚至不能使用 find 來獲取創建日期!

管道(“|”)用於重定向一個命令的輸出以用作另一命令的輸入。

所以對於修改日期,我會使用 -

find mydir -user "$username" -newerat $date ! -newerat $(expr $date + 1)

如果您想擴展功能(例如,能夠使用 yyyy-mm-dd),您必須將日期輸入為 yyyymmdd - 查看sedawkgrep

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM