1

I have a music volume on my NAS which contains plenty of CD's.The file names are typically 01.name.mp3 , 02.name.mp3 etc. On another part of the NAS I store snapshots taken and named at each hour of the day 00-00-00.jpg , 01-00-00.jpg.... 23-00-00.jpg I made a crontab to delete the snapshots before 7am as they were usually dark , so no point in keeping them.

Well guess what , I had a link in snapshots folder which I had forgotten ,pointing to the music files. So after a while I realized most of my music files from 01-name.mp3 to 07-name.jpg had been deleted. What I am trying to do is assess the damage & display only the directories that contain files starting with 09-name.mpg AND do not contain 01-name.mp3.This folder (or cd) has had the files lower than 07 deleted.

Ideally during the search I would like to eliminate any other directory containing a non-numeric name like "main-theme.mp3" as I know these folders are complete.

I did try:

find . -name  "09*" -a ! -name "01*"

but it didn't work. I also tried this which did not work

find . -mindepth 2 -maxdepth 2 -type d '!' -exec test -e "{}/01*" ';' -print

My system:

Linux fut-NUC7i3BNH 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1 03:25:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.4 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
1
  • To start with, which version of Linux have you installed (Ubuntu server, Ubuntu desktop, Kubuntu, Lubuntu, Xubuntu, Ubuntu MATE, et al.) , and which release number? Different releases have different tools for us to recommend. Please click edit and add that vital information to your question so all the facts we need are in the question. Please don't use Add Comment, since that's our channel to you. All facts about your system should go in the Question with edit
    – K7AAY
    Apr 13, 2020 at 17:12

2 Answers 2

0

The next command

  • looks for all unique folders containing files starting with 09*
  • looks for all unique folders containing files starting with 01*
  • removes lines which point to 01* files from output which points to 09* files.

Number of 09* files should be bigger then number of 01* files because a part of them have been removed, so the command will output only folders with 01* files removed.

snpath=$(echo ~/test/snapshots/) && \
find $snpath -name "09*" | sed 's|\(.*\)/.*|\1|' | sort | uniq > allf.txt && \
find $snpath -name "01*" | sed 's|\(.*\)/.*|\1|' | sort | uniq > notallf.txt && \
grep -Fvxf notallf.txt allf.txt | grep -oP "^$snpath\K.*"
  • $ snpath=$(echo ~/test/snapshots/) - full path to snapshots folder
  • find $snpath -name "09*" finds all files starting with 09 with their full paths
  • sed 's|\(.*\)/.*|\1|' strips files names leaving parent directories paths only
  • sort | uniq removes duplicate paths lines leaving only unique directories names
  • find $snpath -name "01*" | sed 's|\(.*\)/.*|\1| finds undesired directories we do not want to see (they do not contain removed files)
  • grep -Fvxf notallf.txt allf.txt removes undesired folders names from all folders' list leaving only folders that contain 09* files names and do not contain 01* files names
  • awk 'NR==FNR{a[$0];next} !($0 in a)' notallf.txt allf.txt could be used instead of previous grep command
  • grep -oP "^$snpath\K.*" path to snapshots folder from output

    $ tree ./
    ./
    ├── allf.txt
    ├── Alligator Records 20th Anniversary Collection
    │   ├── CD1
    │   │   ├── 01 - Hound Dog Taylor And The Houserockers .mp3
    │   │   ├── 02 - James Cotton - No Cuttin Loose.mp3
    │   │   ├── 03 - Black Cat Bone .mp3
    │   │   ├── 04 - Professor Longhair - Big Chief.mp3
    │   │   ├── 05 - Koko Taylor - Thats Why Im Crying.mp3
    │   │   ├── 06 - Tinsley Ellis - Double-eyed Whammy.mp3
    │   │   ├── 07 - Lucky Peterson - Im Free.mp3
    │   │   ├── 08 - A.C. Reed-Stevie Ray Vaughan mp3
    │   │   ├── 09 - Little Charlie And The Nightcats - Rain.mp3
    │   │   └── file1
    │   ├── CD2
    │   │   ├── 09 - Bessie Smith,Moan, You Moaners.mp3
    │   │   ├── 10 - Louis Armstrong,Nobody Knows The Trouble Ive Seen.mp3
    │   │   ├── 11 - Golden Gate Quartet,The Valley Of Time.mp3
    │   │   └── 12 - Golden Gate Quartet,The Sun Didnt Shine.mp3
    │   └── file2
    ├── file
    └── notallf.txt
    
    3 directories, 18 files
    

Result:

$ snpath=$(echo ~/test/snapshots/) && find $snpath -name "09*" | sed 's|\(.*\)/.*|\1|' | sort | uniq > allf.txt && find $snpath -name "01*" | sed 's|\(.*\)/.*|\1|' | sort | uniq > notallf.txt && grep -Fvxf notallf.txt allf.txt | grep -oP "^$snpath\K.*"
Alligator Records 20th Anniversary Collection/CD2
9
  • Hi Gryu,thanks for your answer , I am in awe , I have always been afraid to use sed , but you have it under control !!.Both .txt files are created but there is an error with the last grep command "grep: notallf.txt:4: Invalid range end". Please help
    – Smindle
    Apr 22, 2020 at 17:15
  • Hi Gryu, I did try the fgrep & grep -F.... but no change.
    – Smindle
    Apr 23, 2020 at 15:14
  • I then did a diff on the 2 files allf.txt & notallfiles.txt & they were always the same ?Just for info , the content of allf.txt contained a lot of correct folders with 09 & no 01 , but it also contained a few complete folders with 01 , 02 03....09 . I did look at the folders to see if there were any differences ,some with sub-folders were ok (01-09) others with one subfolder was correctly flagged as not containing any 01 but only a 09 ?
    – Smindle
    Apr 23, 2020 at 15:27
  • Sorry I dont know to share the info.I can ls -R * > tree-structure & upload it but to where ?
    – Smindle
    Apr 23, 2020 at 18:44
  • Alligator Records 20th Anniversary Collection': CD1 CD2 folder.jpeg 'Track List.rtf' 'Alligator Records 20th Anniversary Collection/CD1': '01 - Hound Dog Taylor And The Houserockers .mp3' "02 - James Cotton - No Cuttin' Loose.mp3" '03 - Black Cat Bone .mp3' '04 - Professor Longhair - Big Chief.mp3' "05 - Koko Taylor - That's Why I'm Crying.mp3" '06 - Tinsley Ellis - Double-eyed Whammy.mp3' "07 - Lucky Peterson - I'm Free.mp3" '08 - A.C. Reed-Stevie Ray Vaughan mp3' '09 - Little Charlie And The Nightcats - Rain.mp3'
    – Smindle
    Apr 23, 2020 at 18:51
0

You can do it in two steps: find directories which contain name number one, then find in the output directories which contain name number 2.

Example:

find . -name  "09* > output_first_search
cat output_first_search | grep "01*"
2
  • Hi there Josef,
    – Smindle
    Apr 22, 2020 at 16:33
  • 1
    Thanks.Its so beautifully simple but it doesnt work all the time.I am going through the folders trying to understand when it works & when it doesent....will keep you informed
    – Smindle
    Apr 22, 2020 at 16:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .