I am trying to write a program that will search through a main directories sub-directories and list the files that have the largest number at the end. ex: filename_100.
find . -name "*_*" | sort -n | tail
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
sortsorts starting from the beginning of the string, so you can’t use it without first splitting off the leading part of the filename. This loop will do that; it prints out the part of the filename after the_, followed by the full filename.Then you can pipe the output to
sortandtailto get the largest number, and then tocutto pick out only the filename itself.Then you’ll need to extract the part of the filename before the underscore. This is probably best done by storing the result of the last part in a variable,
after which you can use bash’s suffix removal to strip off the part after the underscore, and then list all files that share that prefix.