简体   繁体   中英

bash script for sorting files

Hi I am new to bash scripting. I am trying to write a code that provides me all the files for the username entered by the user when prompted and the file modified date as entered by the user. I am not able to sort and display the files with the date provided by the user. Please help.

#!/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"

Go through your code one step at a time.

ls -l 

Will print the contents of the directory the script is being run from. You're then sending that output to find , which makes running ls -l redundant because find doesn't take anything from stdin

You're then sending the output of find mypath -user "$username" (which is now just a list of files) to another find command, which again, doesn't take anything from stdin.

Also a quick google of "find created date" returned this thread, which tells us you can't even use find to get the created date!

Pipes ("|") are used for redirecting the output of one command to be used as the input of another command.

So for modified date, I would use -

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

You will have to input the date as yyyymmdd - look in to sed , awk , and grep if you want to expand the functionality (eg. being able to use yyyy-mm-dd).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM