Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
-3
votes
5
answers
69
views
Perl or sed script to remove a specific content of a file from another bigger file
I have a file **(FILE1)** with some repeated sections as below (example): LINE 1 ABCD LINE 2 EFGA LINE 3 HCJK REMOVE LINE11 REMOVE LINE12 REMOVE LINE13 LINE 4 ABCDH LINE 5 EFGAG LINE 6 HCJKD REMOVE LINE11 REMOVE LINE12 REMOVE LINE13 LINE 7 ABCDH LINE 8 EFGAG LINE 9 HCJKD I have several such files. I...
I have a file **(FILE1)** with some repeated sections as below (example):
LINE 1 ABCD
LINE 2 EFGA
LINE 3 HCJK
REMOVE LINE11
REMOVE LINE12
REMOVE LINE13
LINE 4 ABCDH
LINE 5 EFGAG
LINE 6 HCJKD
REMOVE LINE11
REMOVE LINE12
REMOVE LINE13
LINE 7 ABCDH
LINE 8 EFGAG
LINE 9 HCJKD
I have several such files. In a pattern file (**PATTERN**) I have these removable lines stored.
REMOVE LINE11
REMOVE LINE12
REMOVE LINE13
I want to write a sed, awk (bash code) or a Perl code to remove all the sections of of **FILE** that match the content of the file **PATTERN**. Another requirement is to remove all but leave the first occurrence only.
Pratap
(47 rep)
Aug 6, 2025, 06:03 AM
• Last activity: Aug 6, 2025, 03:31 PM
2
votes
2
answers
2272
views
I/O errors and undeletable directories
For some unknown reason, there are 2 directories I can't delete. First directory called **sw.old** is empty and can be deleted only by `rm`, as `rmdir` won't recognize it. However, even after `rm`, it still shows up: [02:11:36] user@user:/media/user/exthdd/docs$ ls -il total 1072064 1456 drwx------...
For some unknown reason, there are 2 directories I can't delete.
First directory called **sw.old** is empty and can be deleted only by
rm
, as rmdir
won't recognize it. However, even after rm
, it still shows up:
[02:11:36] user@user:/media/user/exthdd/docs$ ls -il
total 1072064
1456 drwx------ 1 user user 0 Aug 12 10:04 1old.or.probably.unfinished
5717 drwx------ 1 user user 8192 Jan 27 22:58 videos
6528 -rw------- 1 user user 1097779088 Nov 5 16:15 release_Remix_OS_for_PC_Android_M_64bit_B2016112101.zip
8008 drwx------ 1 user user 4096 Jan 28 00:55 txt
64 drwx------ 1 user user 0 Dec 25 22:15 sw.old
[02:12:03] user@user:/media/user/exthdd/docs$ rmdir sw.old/
rmdir: failed to remove ‘sw.old/’: No such file or directory
[02:12:57] user@user:/media/user/exthdd/docs$ rm -rf sw.old/
[02:13:15] user@user:/media/user/exthdd/docs$ ls -il
total 1072064
1456 drwx------ 1 user user 0 Aug 12 10:04 1old.or.probably.unfinished
5717 drwx------ 1 user user 8192 Jan 27 22:58 videos
6528 -rw------- 1 user user 1097779088 Nov 5 16:15 release_Remix_OS_for_PC_Android_M_64bit_B2016112101.zip
8008 drwx------ 1 user user 4096 Jan 28 00:55 txt
64 drwx------ 1 user user 0 Dec 25 22:15 sw.old
Second one called **misc** has a corrupted file inside it:
[02:24:32] user@user:/media/user/exthdd/docs/txt$ ls -il
total 0
22607 drwx------ 1 user user 0 Dec 31 16:09 misc
[02:24:36] user@user:/media/user/exthdd/docs/txt$ ls -il misc/
ls: cannot access misc/patterns.mp4: Input/output error
total 0
? -????????? ? ? ? ? ? patterns.mp4
[02:24:54] user@user:/media/user/exthdd/docs/txt$ rm -rf misc/
rm: cannot remove ‘misc/patterns.mp4’: Input/output error
How can I remove those directories (and corrupted file inside one of them) without formatting?
PKM
(131 rep)
Jan 28, 2018, 12:40 AM
• Last activity: Jul 28, 2025, 07:02 PM
-2
votes
0
answers
46
views
Deleting immutable distro
I was using windows and tried to install nixos on my external hard drive. After some problems with partitioning the drive stopped being bootable. I want to erase the drive and continue using it but windows does not let me do anything with it because it is read only now. Is there any way to reset it?
I was using windows and tried to install nixos on my external hard drive.
After some problems with partitioning the drive stopped being bootable. I want to erase the drive and continue using it but windows does not let me do anything with it because it is read only now. Is there any way to reset it?
Andrey Maerov
(1 rep)
Jul 8, 2025, 11:38 AM
2
votes
1
answers
1907
views
Cannot delete folder on Synology NAS?
I logged in as root and tried to recursively delete the folder `/volume2/Name` using `rm -rf` It started and deleted all the files/directories inside this folder `Name`, but it cannot remove the folder itself even though I am root: rm: cannot remove 'Name’: Operation not permitted The command `ls -l...
I logged in as root and tried to recursively delete the folder
/volume2/Name
using rm -rf
It started and deleted all the files/directories inside this folder Name
, but it cannot remove the folder itself even though I am root:
rm: cannot remove 'Name’: Operation not permitted
The command ls -l
list the permissions for this folder as:
d---------+ 1 root root
Synology has a special tool called synoacltool
for these ACL permissions, but I don't know to use it.
So what do I need to do in order to be able to remove this (empty) folder?
Maestro
(211 rep)
Nov 23, 2021, 07:00 PM
• Last activity: Jun 29, 2025, 07:06 AM
1
votes
2
answers
2173
views
How to compare two file names
I want to compare two files in a folder and delete the number in the name which is the smaller of the two. For instance, say the names were `yearMonthDay.txt`. I want to compare which is a smaller number and `sudo rm` it. I know I can get the numbers via: ```none find *txt | awk -F'[_.]' '{ print $1...
I want to compare two files in a folder and delete the number in the name which is the smaller of the two.
For instance, say the names were
yearMonthDay.txt
. I want to compare which is a smaller number and sudo rm
it.
I know I can get the numbers via:
find *txt | awk -F'[_.]' '{ print $1}'
How then next should I compare it? Using system variables? I actually haven't used shell variables before.
1toneboy
(465 rep)
Nov 27, 2021, 02:26 AM
• Last activity: Jun 25, 2025, 01:07 PM
2
votes
4
answers
2150
views
How to identify and remove orphaned config files of uninstalled Software?
Programs often store config files in the home directory usually in a hidden directory, often but not limited to sub-directories of ~/.config, ~/.local or ~/.gnome After uninstalling the programs, the config-files are still kept (which usually is preferred). Currently I look through the hidden direct...
Programs often store config files in the home directory usually in a hidden directory, often but not limited to sub-directories of ~/.config, ~/.local or ~/.gnome
After uninstalling the programs, the config-files are still kept (which usually is preferred).
Currently I look through the hidden directories of home from time to time, try to guess where the directories belong to, and delete them, if I'm not using the program anymore. But this is time-consuming and difficult/error-prone, because sometimes it's hard to guess, which program a directory belongs to. I wonder if there is tool or method that could help me here. (Similar to Bleachbit or CCleaner on Win, but afaik they don't have this functionality).
**Is there an easy way to identify and remove orphaned config-directories of uninstalled software?**
**Edit:** I'm using Arch and Ubuntu, but solutions for other distros would be interesting, too. The solution doesn't need to be a user friendly GUI-application (would be nice, though), a CLI command or script would work for me, too.
dynobo
(121 rep)
Dec 17, 2017, 10:37 AM
• Last activity: Jun 5, 2025, 07:07 PM
6
votes
2
answers
1362
views
Deleting a file before it has finished downloading
Today I downloaded a large file using `wget`, but I accidentally deleted it before it had finished downloading. However `wget` continued to download the file until it had finished, but there was no file saved at the end. I wonder what happened to rest of the file that Wget downloaded after I deleted...
Today I downloaded a large file using
wget
, but I accidentally deleted it before it had finished downloading.
However wget
continued to download the file until it had finished, but there was no file saved at the end.
I wonder what happened to rest of the file that Wget downloaded after I deleted it?
EmmaV
(4359 rep)
May 20, 2025, 02:30 PM
• Last activity: May 25, 2025, 08:04 PM
1
votes
1
answers
3008
views
cannot delete files / folders in mounted drives
I have dual booted Windows 10 with Ubuntu on it Ubuntu is on a second hdd partition table: /root /dev/sdb2 30gb /home /dev/sdb3 300gb /swap /dev/sdb4 8gb ntfs drive /dev/sdb5 remaining but I am not able to delete files on the NTFS volume with default account, can delete with root permissions. n...
I have dual booted Windows 10 with Ubuntu on it
Ubuntu is on a second hdd
partition table:
/root /dev/sdb2 30gb
/home /dev/sdb3 300gb
/swap /dev/sdb4 8gb
ntfs drive /dev/sdb5 remaining
but I am not able to delete files on the NTFS volume with default account, can delete with root permissions.
nor can I delete files on the Windows partitions.
I have these permissions (
ls -l
) => -rwxrwxrwx drwxrwxrwx
What should I do?
adi_rocks
(11 rep)
Jun 8, 2018, 05:51 AM
• Last activity: May 15, 2025, 01:06 AM
0
votes
0
answers
73
views
Not able to modify inode metadata
I am working on a ext4 file-system tool that aims to delete files such that they are not recoverable later on. My goal is to clear the inode metadata of a deleted file, but despite my efforts, the changes I make to the inode metadata are not reflecting when I inspect the inode later using stat. Here...
I am working on a ext4 file-system tool that aims to delete files such that they are not recoverable later on. My goal is to clear the inode metadata of a deleted file, but despite my efforts, the changes I make to the inode metadata are not reflecting when I inspect the inode later using stat.
Here's the process I follow to modify the inode:
1. Fetch the inode using ext2fs_read_inode.
2. Modify the inode metadata by setting values like i_mode, i_links_count, etc., to zero or NULL.
3. Write the modified inode back to the filesystem using
ext2fs_write_inode.
void clear_inode_metadata(ext2_filsys &fs, ext2_ino_t inode_num)
{
ext2_inode inode;
ext2fs_read_inode(fs, inode_num, &inode);
inode.i_mode = 0;
inode.i_links_count = 0;
inode.i_size = 0;
inode.i_size_high = 0;
inode.i_blocks = 0;
memset(inode.i_block, 0, sizeof(inode.i_block));
ext2fs_write_inode(fs, inode_num, &inode); // Write the cleared inode back to disk
}
void delete_file(const string &path, ext2_filsys &fs, const string &device)
{
ext2_ino_t inode_num = fetch_file(path, fs, device);
if (inode_num != 0)
{
overwrite_blocks(fs, device, path); // traverses extents and writes random data
clear_inode_metadata(fs, inode_num);
}
}
The inode metadata stays the same even after invoking clear_inode_metadata()
.
Also I have a file recovery module that uses a disk snapshot to recover files and to my surprise it is able to recover the file even after I overwrite the file using overwrite_blocks()
. The snapshot stores extents used by an inode and the total size of the file.
My questions:
1. Why aren't the changes to the inode metadata reflecting after calling ext2fs_write_inode?
2. Why is the file recovery tool still able to recover the file after I overwrite its blocks?
Dhruv
(1 rep)
Apr 27, 2025, 08:14 AM
• Last activity: Apr 27, 2025, 08:16 AM
2
votes
4
answers
7031
views
Delete files with specific content
Having a thousand of data files (same extension) in a directory, how to delete those which contain a specific parameter statement? For example, if I wented to delete all files containing the line ``` Key = 0 ``` could I do something like ``` grep -i "Key = 0" * | rm * ```
Having a thousand of data files (same extension) in a directory, how to delete those which contain a specific parameter statement?
For example, if I wented to delete all files containing the line
Key = 0
could I do something like
grep -i "Key = 0" * | rm *
André Fonseca
(31 rep)
Aug 11, 2020, 07:11 PM
• Last activity: Feb 16, 2025, 02:42 PM
1
votes
2
answers
1894
views
rsync-like --delete functionality with wget (not wget's --delete-after)
**Background:** my particular problem, which gave rise to this question, is as follows. I'm a slackware linux user, and on 23-March-2019 mirrored their distribution with the following command wget -r -np -R "index.html*" https://mirror.slackbuilds.org/slackware/slackware64-current/ Then recently, 29...
**Background:** my particular problem, which gave rise to this question, is as follows. I'm a slackware linux user, and on 23-March-2019 mirrored their distribution with the following command
wget -r -np -R "index.html*" https://mirror.slackbuilds.org/slackware/slackware64-current/
Then recently, 29-Aug-2019, I refreshed/updated my local mirror simply by adding the
**Edit: lengthy reply to @roaima's comment...**
Thanks for the suggestion, @roaima. And now that you mention it, yup, there is such a file in the top-level directory, predictably named FILELIST.TXT But I'm not sure how to use it as input to some procedure that would --delete the older files not in the current filelist. Could you point me to a manpage, or whatever, that describes how to do that? Thanks again. Also, that FILELIST.TXT is in an
-N
option to the above command. But that resulted in my mirror containing many, many "duplicate" older and newer versions of the same packages, just with different version numbers, e.g.,
SDL2_mixer-2.0.4-x86_64-1.txz
SDL2_mixer-2.0.4-x86_64-2.txz
libcddb-1.3.2-x86_64-5.txz
libcddb-1.3.2-x86_64-6.txz
etc (and I mean _**lots**_ of etc's:)
**Question:** So what I really want to do is something like
rsync -av --delete https://mirror.slackbuilds.org/slackware/slackware64-current/ my-slackware64-mirror-directory/
That rsync --delete
would have automatically deleted all the older versions from my mirror-directory that no longer exist on slackbuilds.org. However, I don't have any kind of account on slackbuilds, and therefore can't (as far as I know) run rsync to get files from it. Is there any wget way to accomplish the same thing? Or any way at all? Thanks.
**Edit: lengthy reply to @roaima's comment...**
Thanks for the suggestion, @roaima. And now that you mention it, yup, there is such a file in the top-level directory, predictably named FILELIST.TXT But I'm not sure how to use it as input to some procedure that would --delete the older files not in the current filelist. Could you point me to a manpage, or whatever, that describes how to do that? Thanks again. Also, that FILELIST.TXT is in an
ls -al
format that might not be the easiest for canned procedures to parse (although I can probably write a small C program to convert it to any suitable format). A few typical lines from the file are
-rw-r--r-- 1 root root 1637708 2019-08-15 18:06 ./slackware64/a/bash-5.0.009-x86_64-1.txz
-rw-r--r-- 1 root root 163 2019-08-15 18:06 ./slackware64/a/bash-5.0.009-x86_64-1.txz.asc
-rw-r--r-- 1 root root 226 2018-10-17 03:06 ./slackware64/a/bin-11.1-x86_64-3.txt
-rw-r--r-- 1 root root 39576 2018-10-17 03:06 ./slackware64/a/bin-11.1-x86_64-3.txz
-rw-r--r-- 1 root root 163 2018-10-17 03:06 ./slackware64/a/bin-11.1-x86_
And very relevant to your apparently-prescient suggestion, the first few lines of the file are the remark
Wed Aug 28 21:44:15 UTC 2019
Here is the file list for this directory. If you are using a
mirror site and find missing or extra files in the disk
subdirectories, please have the archive administrator refresh
the mirror.
John Forkosh
(242 rep)
Aug 30, 2019, 04:47 AM
• Last activity: Feb 10, 2025, 01:42 PM
2
votes
3
answers
27835
views
cannot find Trash folder! Ubuntu 18.04
I am running a preinstalled version of ubuntu 18.04 but cannot find Trash folder. The typical places don't have the folder. ~/.local/share/Trash <-- does not exist /root/..../Trash <-- does not exit The directory should most likely be here but it's not /home/$USER/.local/share/Trash <---does not exi...
I am running a preinstalled version
of ubuntu 18.04 but cannot find Trash folder.
The typical places don't have the folder.
~/.local/share/Trash <-- does not exist
/root/..../Trash <-- does not exit
The directory should most likely be here but it's not
/home/$USER/.local/share/Trash <---does not exist
Hmm ... so I ran a search as follows:
sudo find / -type d -iname Trash
The output was not nice:
find: ‘/run/user/1001/gvfs’: Permission denied
/home/$USER/.local/share/evolution/..../trash
/home/$USER/.cache/evolution/.../trash
/user/share/doc/HTML/.../trash
and that's it!
By
...
I meant there are various directories ending with /trash. But that's useless to mention anyhow since we are not really finding the TRASH can.
(Note that I do see a Trash bin on my Desktop, which is ironic. And yes it is empty.)
Question is, where is my TRASH FOLDER ?
And why do I receive the following output when I run the find command
find: ‘/run/user/1001/gvfs’: Permission denied
sam
(29 rep)
Mar 16, 2019, 03:15 PM
• Last activity: Jan 16, 2025, 07:31 AM
0
votes
1
answers
102
views
Deleting a directory
I am working through a tutorial - The Linux Command Line for Beginners - and I want to delete my work so far and start over. I have a directory called `tmp` within the root directory which has a solid green box around it. If I `ls` this directory I get a config error. I don't know what this means an...
I am working through a tutorial - The Linux Command Line for Beginners - and I want to delete my work so far and start over. I have a directory called
tmp
within the root directory which has a solid green box around it. If I ls
this directory I get a config error. I don't know what this means and I can't delete it.
Can you help please?
peter@peter-HP-ProDesk-400-G5-MT:~$ cd /
peter@peter-HP-ProDesk-400-G5-MT:/$ ls
bin dev lib libx32 mnt root srv timeshift var
boot etc lib32 lost+found opt run swapfile tmp
cdrom home lib64 media proc sbin sys usr
peter@peter-HP-ProDesk-400-G5-MT:/$ cd tmp
peter@peter-HP-ProDesk-400-G5-MT:/tmp$ ls
config-err-MUm7iD
mintUpdate
MozillaUpdateLock-4F96D1932A9F858E
systemd-private-bc10ea19133a48fd8e526c45b179e6cf-colord.service-CPuYYO
systemd-private-bc10ea19133a48fd8e526c45b179e6cf-ModemManager.service-dUDoww
systemd-private-bc10ea19133a48fd8e526c45b179e6cf-switcheroo-control.service-Z8zyOX
systemd-private-bc10ea19133a48fd8e526c45b179e6cf-systemd-logind.service-7z9IHP
systemd-private-bc10ea19133a48fd8e526c45b179e6cf-systemd-resolved.service-vLAsUl
systemd-private-bc10ea19133a48fd8e526c45b179e6cf-upower.service-fxuTWM
tutorial
I have copied the screen as above but note:
the directory entitled 'tmp' has a green solid box over it but is not shown on this screen grab.
Also the word 'mintUpdate' shown in the output of running ls
int he tmp/
directory has a solid green box not shown.
Pete Sweet
(11 rep)
Dec 16, 2024, 11:12 AM
• Last activity: Dec 16, 2024, 09:03 PM
0
votes
3
answers
2153
views
Quicker alternative to Linux Wipe Command for secure deletion
I am fairly new to Linux but all I can say is I love it!!! :D Anyways, I am trying to securely erase 1000s of files from my harddrive because new year is approaching and I want a fresh start to life. As in remove toxic files, toxic photos etc I am currently using the Linux `wipe` command as in `sudo...
I am fairly new to Linux but all I can say is I love it!!! :D
Anyways, I am trying to securely erase 1000s of files from my harddrive because new year is approaching and I want a fresh start to life. As in remove toxic files, toxic photos etc
I am currently using the Linux
wipe
command as in sudo wipe -rcf
but it is taking FOREVER.
I believe because it is doing a wipe 34 times according to Guttmans Algorithm
. I appreciate it is good but are there any quicker alternatives that are safe deletion. I am new to it all as in cyber security but I learnt worrying enough that doing the rm
command on Linux or even permanently deleting from Windows recycle bin isn't safe and is still recoverable as it just hides the files. Hell I am recovering old files haha of my younger self
n00bl1nux
(33 rep)
Dec 24, 2019, 01:44 AM
• Last activity: Nov 20, 2024, 05:00 AM
6
votes
4
answers
9622
views
Delete files and folders recursively in subdirectories
There is a folder, "transfer". In that transfer folder there are user folders "user1", "user2", etc. I want to (periodically) delete the content (i.e. all files and folders in the user folders) but I do not want to delete the "transfer" or user folders. How can I do that using as shell script/comman...
There is a folder, "transfer". In that transfer folder there are user folders "user1", "user2", etc. I want to (periodically) delete the content (i.e. all files and folders in the user folders) but I do not want to delete the "transfer" or user folders.
How can I do that using as shell script/command without manually adding a call for each new user folder every time I add a new user?
Silicomancer
(225 rep)
Nov 12, 2019, 08:18 PM
• Last activity: Oct 22, 2024, 02:01 PM
3
votes
3
answers
250
views
Find and delete files from unix directory of multiple patterns
I need to delete files from a unix directory having patterns like 'bkp', 'backup', 'bak'. Basically I want to delete all the backup files. Files in the directory: ```` A001.txt_bkp A001.txt A002.csv_backup A002.csv A003.csv A003_bak.csv ```` I want to keep below files ```` A001.txt A002.csv A003.csv...
I need to delete files from a unix directory having patterns like 'bkp', 'backup', 'bak'.
Basically I want to delete all the backup files.
Files in the directory:
`
A001.txt_bkp
A001.txt
A002.csv_backup
A002.csv
A003.csv
A003_bak.csv
`
I want to keep below files
`
A001.txt
A002.csv
A003.csv
`
Mini
(41 rep)
Sep 2, 2024, 06:46 PM
• Last activity: Sep 3, 2024, 10:37 PM
3
votes
2
answers
1433
views
Why does `find` not delete the files it found?
I'm trying to understand why the `find` command is not deleting files in one particular case. I've got the following command: find C:\\path\\to\\Pictures\\pmcctv -name cap_*.jpg -o -name cap_*.ogg -o -name cap_*.flv -o -name cap_*.mp4 -o -name cap_*.webm -mtime +7 Which returns the files I would exp...
I'm trying to understand why the
find
command is not deleting files in one particular case. I've got the following command:
find C:\\path\\to\\Pictures\\pmcctv -name cap_*.jpg
-o -name cap_*.ogg -o -name cap_*.flv -o -name cap_*.mp4 -o -name cap_*.webm -mtime +7
Which returns the files I would expect:
C:\path\to/Pictures/pmcctv/cap_20160915T193251_620067800.jpg
C:\path\to/Pictures/pmcctv/cap_20160915T193322_742708800.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T081046_394767500.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T081125_615129600.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T081129_503678200.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T081255_842394600.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161008_693586800.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161012_749396800.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161043_774132200.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161150_497251900.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161154_437379600.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161337_350955300.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161341_452596000.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161412_870774000.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161443_969064400.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161554_468109900.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161558_378086000.jpg
C:\path\to/Pictures/pmcctv/cap_20160916T161916_668433100.jpg
Now if I try to run the same command with -delete
, the files are not deleted:
find C:\\path\\to\\Pictures\\pmcctv -name cap_*.jpg -o -name cap_*.ogg
-o -name cap_*.flv -o -name cap_*.mp4 -o -name cap_*.webm -mtime +7 -delete
I've also tried with -exec rm
, but they are also not deleted:
find C:\\path\\to\\Pictures\\pmcctv -name cap_*.jpg -o -name cap_*.ogg
-o -name cap_*.flv -o -name cap_*.mp4 -o -name cap_*.webm -mtime +7 -exec rm {} \;
find
returns no error and the files are owned by me with permissions 644.
Any idea what could be causing this?
(note: I've cut the commands to make it more readable but normally there's no line break)
laurent
(2068 rep)
Sep 18, 2016, 02:46 PM
• Last activity: Aug 28, 2024, 01:18 PM
0
votes
1
answers
132
views
Why does "find . -type d -empty -mtime +7 -delete" produce "No such file or directory" error messages?
I've found similar questions being answered, where the find command encounters errors deleting files. However, the most similar question with an answer on Stack Exchange is regarding the `find -type d -exec rmdir {} \;` command. People have recommended using the `find` command with `-delete` instead...
I've found similar questions being answered, where the find command encounters errors deleting files. However, the most similar question with an answer on Stack Exchange is regarding the
find -type d -exec rmdir {} \;
command. People have recommended using the find
command with -delete
instead because the -delete
option implies -depth
, which has the find
command use depth-first search to delete files.
That makes sense to me, but I am using the command people recommend find . -type d -empty -mtime +7 -delete
to delete empty directories older than 7 days old. Yet, I still encounter the error No such file or directory
.
Interestingly, when I go to the directories that do not exist, I cannot find them. Is it deleting the directories despite the error?
Mory
(1 rep)
Aug 27, 2024, 07:54 PM
• Last activity: Aug 28, 2024, 02:13 AM
0
votes
2
answers
259
views
To make 'rmdir' and 'find -delete' ignore '.DS_Store' files
On macOS, there is a hidden file `.DS_Store` in some directories (well, in most of them). The problem with `.DS_Store` files is that `rmdir` and `find . -depth 1 -type d -empty -print -delete` don't delete such directories. I suspect there is no straightforward way to handle this, such as nonexisten...
On macOS, there is a hidden file
.DS_Store
in some directories (well, in most of them).
The problem with .DS_Store
files is that rmdir
and find . -depth 1 -type d -empty -print -delete
don't delete such directories.
I suspect there is no straightforward way to handle this, such as nonexistent --ignore_ds_store
option of rmdir
, so maybe there is a workaround. The default macOS shell is zsh
, so maybe the workaround is possible using some of zsh
features; so I added the 'zsh' tag to the question.
jsx97
(1347 rep)
Aug 16, 2024, 07:44 PM
• Last activity: Aug 18, 2024, 09:29 AM
56
votes
7
answers
93953
views
Delete all folders inside a folder except one with specific name
I need to delete all folders inside a folder using a daily script. The folder for that day needs to be left. Folder 'myfolder' has 3 sub folder: 'test1', 'test2' and 'test3' I need to delete all except 'test2'. I am trying to match exact name here: find /home/myfolder -type d ! -name 'test2' | xargs...
I need to delete all folders inside a folder using a daily script. The folder for that day needs to be left.
Folder 'myfolder' has 3 sub folder: 'test1', 'test2' and 'test3'
I need to delete all except 'test2'.
I am trying to match exact name here:
find /home/myfolder -type d ! -name 'test2' | xargs rm -rf
OR
find /home/myfolder -type d ! -name 'test2' -delete
This command always tries to delete the main folder 'myfolder' also !
Is there a way to avoid this ?
Riju Mahna
(693 rep)
Feb 6, 2018, 08:59 PM
• Last activity: Jul 25, 2024, 08:20 AM
Showing page 1 of 20 total questions