#] #] ********************* #] "$d_SysMaint"'Linux/rm notes.txt' - safe removal of [file, dir]s # www.BillHowell.ca 15Jul2018 initial rm doesn't like ???wildcards (asterixs) within quotation marks???!!! - must use find To remove files in a directory (but NOT sub-directories!) : $ find "/home/bill/Thunderbird/n4caryuo.default/Mail/Local Folders/" -maxdepth 1 -type f | grep "0_Follow_up.msf" | tr \\n \\0 | xargs -0 rm >> test OK $ find "/home/bill/Thunderbird/n4caryuo.default/Mail/Local Folders/" -maxdepth 1 -type f | grep ".msf" | tr \\n \\0 | xargs -0 rm >> OK,all .msf deleted in "Local Folders" directory, but not subdirs tO REMOVE DIRECTORY AND ALL [files, sub-directories] : $ rm -rf "/media/bill/SAMSUNG_G4/IJCNN2019/" #24************************24 #24************************24 # List of operators, generated with : # $ grep "^#]" "$d_SysMaint"'Linux/rm notes.txt' | sed "s/^#\] / /" ********************* "$d_SysMaint"'Linux/rm notes.txt' - safe removal of [file, dir]s 01Sep2023 "$d_PROJECTS"'webHtmlOnly/': $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive$\|z_Old$" | tr \\n \\0 | xargs -0 -IFILE rm -r "FILE" check result : $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive$\|z_Old$" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' 19Aug2023 find -> rm a file 27Jul2022 redo transfer of files to new Seagate_6Tb HDD drive backup_USBdrive_to_ThinkPad - 24Ma2021 d_webSite - put on USB memory stick to avoid accidental [usage, edit, delete]s etc of d_website rather than d_webRawe 15Jul2018 rm files in a directory, must use recursion option for subdirs #24************************24 08********08 #] ??Jan2024 08********08 #] ??Jan2024 08********08 #] ??Jan2024 08********08 #] ??Jan2024 08********08 #] ??Jan2024 08********08 #] ??Jan2024 08********08 #] ??Jan2024 08********08 #] ??Jan2024 08********08 #] 26Jan2024 rm a directory recursively, one that has file locks $ sudo rm -r "$d_web"'ProjMini/Kaal- Structured Atom Model/' $ sudo rm -r '/media/bill/WD6Tb_240124/web/ProjMini/Kaal- Structured Atom Model/' >> done 08********08 #] 12Oct2023 dWebPage_upload_online() - rm all files (not dirs) from d_webHtmlOnly see "$d_PROJECTS"'bin - secure/lftp update entire webSite.sh' # clear all files (not dirs) from d_webHtmlOnly find "$d_webOnly" -type f -print0 | xargs -0 rm -f 08********08 #] 01Sep2023 "$d_PROJECTS"'webHtmlOnly/': see "$d_bin""rsync directories.sh" #] rm "z_Archive$\|z_archive$\|z_Old$\|z_old$" #] rm files without extensions # see "$d_bin"'backup fix screwup notes.txt' # backer_htmlOnly() : # rm "z_Archive$\|z_archive$\|z_Old$\|z_old$", also high-volume dirs # check result : # $ find "$d_PROJECTS""webHtmlOnly/" -type d | grep "z_Archive$\|z_archive$\|z_Old$\|z_old$\|System_maintenance\/tex$\|ProjMajor\/OPM\/OPM\/matlab_mrst$\|ProjMajor\/OPM\/OPM/Octave$\|References\/Climategate $\|garrybog\/pine$" >"$d_temp"'rm webHtmlOnly z_[Archive, Old, high-volume, etc].txt' # rm dirs : # $ find "$d_PROJECTS""webHtmlOnly/" -type d | grep "z_Archive$\|z_archive$\|z_Old$\|z_old$\|System_maintenance\/tex$\|ProjMajor\/OPM\/OPM\/matlab_mrst$\|ProjMajor\/OPM\/OPM/Octave$\|References\/Climategate $\|garrybog\/pine$" | tr \\n \\0 | xargs -0 -IFILE rm -r "FILE" # rm files without extension # examples - see "$d_temp"'rm webHtmlOnly files without extension list.txt' # eg '/home/bill/PROJECTS/webHtmlOnly/economics, markets/Econometric models/Indexes' # check result : # $ find "$d_PROJECTS""webHtmlOnly/" -type f | grep "\/[A-Za-z0-9]*$" >"$d_temp"'rm webHtmlOnly files without extension list.txt' # rm files without extension # $ find "$d_PROJECTS""webHtmlOnly/" -type f | grep "\/[A-Za-z0-9]*$" | tr \\n \\0 | xargs -0 -IFILE rm -r "FILE" >> added to "$d_SysMaint"'Linux/rm notes.txt' +-----+ $ find "$d_PROJECTS"'webHtmlOnly/' -type d -name "z_Archive" | grep "z_Old" | grep --invert-match "z_Archive" >"$d_temp"'rm webHtmlOny z_[Archive, Old]' | tr \\n \\0 | xargs -0 -IFILE --recursive rm "FILE" $ find "$d_PROJECTS"'webHtmlOnly/' -type d -name "*/" | grep "z_Archive\|z_Old" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' find: warning: ‘-name’ matches against basenames only, but the given pattern contains a directory separator (‘/’), thus the expression will evaluate to false all the time. Did you mean ‘-wholename’? $ find "$d_PROJECTS"'webHtmlOnly/' -type d -wholename "*/" | grep "z_Archive\|z_Old" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive$\|z_Old$" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' >> finally, this works $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive$\|z_Old$" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' | tr \\n \\0 | xargs -0 -IFILE --recursive rm "FILE" xargs: unrecognized option '--recursive' Try 'xargs --help' for more information. $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive$\|z_Old$" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' | tr \\n \\0 | xargs -0 -IFILE -r rm "FILE" >> runs, now check for z_{Archive, Old] $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive$\|z_Old$" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' >> nope, didn't work switch position of -r : $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive$\|z_Old$" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' | tr \\n \\0 | xargs -0 -IFILE rm -r "FILE" >> still doesn't work >> oops file redirection shouldn't be there! $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive$\|z_Old$" | tr \\n \\0 | xargs -0 -IFILE rm -r "FILE" check result : $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive$\|z_Old$" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' >> works - manually check result in "$d_PROJECTS"'webHtmlOnly/' >> manual check: none found, so OK? search "Linux find and list only directories" +-----+ https://unix.stackexchange.com/questions/46244/get-a-list-of-directory-names-with-find Get a list of directory names with find Asked 11 years ago Modified 2 years, 1 month ago Viewed 177k times +--+ With GNU find you can use the -printf option: find . -maxdepth 1 -type d -printf '%f\n' As noted by Paweł in the comments, if you don't want the current directory to be listed add -mindepth 1, e.g.: find . -mindepth 1 -maxdepth 1 -type d -printf '%f\n' edited Sep 26, 2017 at 10:48 answered Aug 24, 2012 at 14:59 Thor $ find "$d_PROJECTS"'webHtmlOnly/' -type d | grep "z_Archive\|z_Old" >"$d_temp"'rm webHtmlOny z_[Archive, Old].txt' >> still lists all files... search "Linux rm and remove directories" +-----+ https://phoenixnap.com/kb/remove-directory-linux How to Remove a Directory in Linux October 21, 2021 How to Remove a Directory in Linux? There are two Linux commands you can use to remove a directory from the terminal window or command line: The rm command removes complete directories, including subdirectories and files. The rmdir command removes empty directories. It is important to note that the rm and rmdir commands permanently remove directories without moving them to the Trash directory. This means that you cannot restore a directory removed using these commands. 08********08 #] 19Aug2023 find -> rm a file $ find "$d_ProjMini" -type f -name "p002fig01.01 Seeing an object vs knowing what it is.html" | tr \\n \\0 | xargs -0 -IFILE rm "FILE" $ find "$d_ProjMini" -type f -name "*.html" | tr \\n \\0 | xargs -0 -IFILE rm "FILE" 08********08 #] 27Jul2022 redo transfer of files to new Seagate_6Tb HDD drive copy with dolphin puts ALL [file, dir]s to date-of-transfer (like cp usually does!!) only use rsync!! +-----+ Rational for new HDD : Gb transferred : PROJECTS 250.6 10:34 81.8; 11:17 167.0; 11:41 214.3; 11:55 241.1; 12:00E done webRawe 25.4 start ~12:15; 12:20E 12.6; Evolution 3.6 Thunderbird 10.0 ThinkPad '/' 177.6 (= 458.0-280.4) Total 467.2 Total is > ThinkPad HDD 458.0, ignoring new [software, project, etc]s However, /run/media/bill/ 154.1 Gb much of which may be redundant, old programs not used even if zero - still not much headspace I like : security advantage of a separate USB HDD easier to startup a new computer easier to switch to a new SSD can put VIDEOS on new Seagate_6Tb drive but can't use it on a different computer, even Linux +-----+ from: /run/media/bill/WD_3.6Tb/[PROJECTS, webRawe, Evolution, Thunderbird] to: /run/media/bill/9e05b040-df7b-4e71-936b-1f7c459b8842/ first delete all copy-pastes from yesterday (4 hours wasted!) $ rm -rf "/run/media/bill/9e05b040-df7b-4e71-936b-1f7c459b8842/PROJECTS/" $ rm -rf "/run/media/bill/9e05b040-df7b-4e71-936b-1f7c459b8842/Website - raw/" $ rm -rf "/run/media/bill/9e05b040-df7b-4e71-936b-1f7c459b8842/Evolution/" $ rm -rf "/run/media/bill/9e05b040-df7b-4e71-936b-1f7c459b8842/Thunderbird/" +-----+ now rsync!! - adapt core rsync command : done $ partition='PROJECTS' $ partition='webRawe' rename simpler for scripts! (was 'Website - raw') $ partition='Evolution' $ partition='Thunderbird' $ rsync --stats '-rltgu' '/run/media/bill/WD_3.6Tb/'"$partition/" '/run/media/bill/9e05b040-df7b-4e71-936b-1f7c459b8842/'"$partition/" >>'/run/media/bill/WD_3.6Tb/Website - raw/bin/backupper/'"$partition"' log cumulative.txt' +-----+ earlier attempts - no workee added [Evolution, Thhunderbird] to : #] backup_USBdrive_to_ThinkPad - backup_USBdrive_to_ThinkPad() { partition="webRawe" log_title="backer_rsync() - rsync USBbackup to $partition of $Howell_computer, " d_src="$d_backupDrive$partition/" d_out="$d_webRawe" backer_rsync partition="PROJECTS" log_title="backer_rsync() - rsync USBbackup to $partition of $Howell_computer, " d_src="$d_backupDrive$partition/" d_out="$d_PROJECTS" backer_rsync partition="Evolution" log_title="backer_rsync() - rsync USBbackup to $partition of $Howell_computer, " d_src="$d_backupDrive$partition/" d_out="$d_Evolution" backer_rsync partition="Thunderbird" log_title="backer_rsync() - rsync USBbackup to $partition of $Howell_computer, " d_src="$d_backupDrive$partition/" d_out="$d_Thunderbird" backer_rsync } >>> NYET!! log file won't work, let's see if it does the job $ bash '/run/media/bill/WD_3.6Tb/Website - raw/bin/backup.sh' had to temporarily reassign : $ d_bin='/run/media/bill/WD_3.6Tb/Website - raw/bin/' >> doesn't work without existing d_webRawe 08********08 #] 24Ma2021 d_webSite - put on USB memory stick to avoid accidental [usage, edit, delete]s etc #] of d_website rather than d_webRawe see "$d_SysMaint""0_LMDE notes.txt" just use gparted to reformat USB drive!!! 08********08 #] 15Jul2018 rm files in a directory, must use recursion option for subdirs example : $ rm "/media/bill/RaspPi_ext4_32Gb/temp/*" rm: cannot remove ‘/media/bill/RaspPi_ext4_32Gb/temp/*’: No such file or directory $ rm "/media/bill/RaspPi_ext4_32Gb/temp" rm: cannot remove ‘/media/bill/RaspPi_ext4_32Gb/temp’: Is a directory $ rm "/media/bill/RaspPi_ext4_32Gb/temp/" rm: cannot remove ‘/media/bill/RaspPi_ext4_32Gb/temp/’: Is a directory $ rm -f "/media/bill/RaspPi_ext4_32Gb/temp/" rm: cannot remove ‘/media/bill/RaspPi_ext4_32Gb/temp/’: Is a directory $ rm "/media/bill/RaspPi_ext4_32Gb/temp/" $ cd "/media/bill/RaspPi_ext4_32Gb/temp/" /media/bill/RaspPi_ext4_32Gb/temp $ rm ./* >> OK - works!!! >> DON'T use ../* !!! or will lose files in upper directory!!!!!!! But this ISN'T good enough! I need to run on directory specification!!!! $ cd ~ ~ $ rm -f "/media/bill/RaspPi_ext4_32Gb/temp/*" +-----+ https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory To delete hidden files, you have to specify: rm -r images/* images/.* With shells whose globs include . and .., this will lead to an error like rm: cannot remove `.' directory `images/.' rm: cannot remove `..' directory `images/..' but it will delete hidden files. An approach without errormessage is to use find/delete with mindepth. This is gnu-find. find images -mindepth 1 -delete Your find may lack the -mindepth or -delete predicate, in which case, you could do: find images/. ! -name . -prune -exec rm -rf {} + shareimprove this answer edited Aug 14 '17 at 13:12 Stéphane Chazelas 274k51505831 answered May 4 '11 at 14:58 +-----+ Try : ~ $ rm -f "/media/bill/RaspPi_ext4_32Gb/temp/*" >> no error message, but files not removed? ~ $ rm -f "/media/bill/RaspPi_ext4_32Gb/temp/*.*" >> Again, no error message, but files not removed? +-----+ https://askubuntu.com/questions/60228/how-to-remove-all-files-from-a-directory To remove the folder with all its contents(including all interior folders): rm -rf /path/to/directory To remove all the contents of the folder(including all interior folders) but not the folder itself: rm -rf /path/to/directory/* To remove all the "files" from inside a folder(not removing interior folders): rm -f /path/to/directory/* Where: rm - stands for "remove" -f - stands for "force" which is helpful when you don't want to be asked/prompted if you want to remove an archive, for example. -r - stands for "recursive" which means that you want to go recursively down every folder and remove everything. shareimprove this answer edited Aug 29 '17 at 7:28 muru 126k19261445 answered Sep 7 '11 at 17:26 Lilian A. Moraru 3,83421819 +---+ If you want to delete only files in /path/to/directory you can do find /path/to/directory -type f -print0| xargs -0 rm or find /path/to/directory -type f -exec rm '{}' \; You can do loads with find, the advantage is you can list what is found without piping it to rm so you can see what will be deleted before you start. shareimprove this answer edited Nov 20 '16 at 6:41 αғsнιη 22.5k2187149 answered Sep 6 '11 at 12:13 Richard Holloway +-----+ $ find "/media/bill/RaspPi_ext4_32Gb/temp" -type f /media/bill/RaspPi_ext4_32Gb/temp/110327 RT@hayano - Graph update, Radiation level in Miyagi-Sendai area, Japan.jpg /media/bill/RaspPi_ext4_32Gb/temp/110313 ABC news - tsunami effect in Japan.htm /media/bill/RaspPi_ext4_32Gb/temp/110914 OC Transpo systemmap.pdf /media/bill/RaspPi_ext4_32Gb/temp/110327 NatPost Three Mile Island meltdown opinion.odt /media/bill/RaspPi_ext4_32Gb/temp/0_References - Toshiba.ods $ find "/media/bill/RaspPi_ext4_32Gb/temp" -type f | xargs rm >> mess as strings treated as sequence of words $ find "/media/bill/RaspPi_ext4_32Gb/temp" -type f | xargs rm from "/media/bill/HOWELL_BASE/System_maintenance/Linux/xargs notes.txt" : | tr \\n \\0 $ find "/media/bill/RaspPi_ext4_32Gb/temp" -type f | tr \\n \\0 | xargs -0 rm >> this worked!!!! # enddoc