Hot answers tagged

66 votes
Accepted

Delete all folders inside a folder except one with specific name

This will delete all folders inside ./myfolder except that ./myfolder/test2 and all its contents will be preserved: find ./myfolder -mindepth 1 ! -regex '^./myfolder/test2\(/.*\)?' -delete How it ...
John1024's user avatar
  • 74k
45 votes
Accepted

Unable to delete this file as root

The file has the i ("immutable") attribute, according to the output from lsattr that you show. From the chattr(1) manual (on Ubuntu): A file with the i attribute cannot be modified: it ...
Kusalananda's user avatar
  • 327k
39 votes
Accepted

Get list of files deleted by `find -delete`

Just add -print to the end of the command: find . -name .DS_Store -delete -print This would output the pathnames of the files found. If this is on a macOS system (or on any of the BSD systems), using ...
Kusalananda's user avatar
  • 327k
31 votes

How do I delete a directory that contains only '.' and '..', but rm says is not empty?

Your directory looks empty, but the ls output indicates that there is a file in there since the link count for the directory is 3 rather than 2 (an empty directory on an APFS filesystem should have a ...
Kusalananda's user avatar
  • 327k
26 votes

Delete files with names that appear to begin with '?' in command line

The appropriate way to remove these kind of files is by using the inode value of the file. Use the following command to get inode value ls -li 12582925 -rw-r--r-- 1 root root 646 May 23 02:...
Siva's user avatar
  • 9,067
23 votes
Accepted

Delete a file called '.env'$'\r'

On recent-ish Linux systems (with GNU tools as in most desktop distributions), ls prints names with weird characters using the shell's quoting syntax. If that '.env'$'\r' is what ls gives, the name of ...
ilkkachu's user avatar
  • 135k
23 votes

Delete a file called '.env'$'\r'

If using GNU find, then you can delete it by the inode. ls -lid -- .env* The number in the first column is the inode. Note what it is for the file that you actually want should others with the string ...
Nasir Riley's user avatar
  • 10.9k
19 votes
Accepted

How to delete all except for some specific folder under parent-folder

You can use find with a negation (at your own risk). find all file and folders named "a" or "b": find -name a -o -name b find all files and folders name "a" or "b" in the current directory" find -...
Scantlight's user avatar
18 votes

Delete all folders inside a folder except one with specific name

Using bash: shopt -s extglob rm -r myfolder/!(test2)/ Example: $ tree myfolder/ myfolder/ ├── test1 │   └── file1 ├── test2 │   └── file2 └── test3 └── file3 $ echo rm -r myfolder/!(test2) rm -...
Jeff Schaller's user avatar
  • 66.7k
18 votes
Accepted

Delete files with names that appear to begin with '?' in command line

The character is not a question mark. The ls utility will replace non printable characters with ?. It is further unclear whether the non printable character really is the first character in the ...
Kusalananda's user avatar
  • 327k
17 votes
Accepted

Delete files and folders recursively in subdirectories

You can do it using following find command: find /path/to/transfer -mindepth 2 -delete -mindepth 2 parameter tells find to ignore first two level of directories: searched directory itself, and all ...
mrc02_kr's user avatar
  • 1,993
15 votes
Accepted

rsync with delete option, but preserve what's deleted

You want the -b option (which causes rsync to backup deleted stuff) combined with the --backup-dir option, which will let you direct WHERE rsync backs things up.
Michael Kohne's user avatar
13 votes

Delete a file called '.env'$'\r'

Ask the shell to wildcard the tricky part of the name, and ask rm to remember it for you so you don't need to type it. $ touch '.env'$'\r' $ ls -l .env* -rw-rw-r-- 1 paul paul 0 May 20 16:51 '.env'$'\...
Paul_Pedant's user avatar
  • 8,431
12 votes
Accepted

Delete all files within a directory, without deleting the directory

If you want to delete a directory's contents and not the directory itself, all you need to do is tell rm to delete the contents: rm inner/* That will delete all non-hidden files in ./inner and leave ...
terdon's user avatar
  • 238k
11 votes

How do I delete a directory in Centos 7

You can use rm -f -r john It will recursively delete john directory even if it contains files or subdirectories.
Slavik Svyrydiuk's user avatar
11 votes

Delete a file called '.env'$'\r'

A quick-and-dirty way to select pesky filenames is to use the autocompletion feature of the shell. Type rm , then hit the TAB key until the file to remove is printed on screen.
dr_'s user avatar
  • 29.2k
10 votes
Accepted

Recursively cleanup all folders and sub-folders in a folder that have no files in them

See if this does what you want: find mainFolder -depth -empty -type d -exec rmdir {} \; That should find directories in mainFolder using a depth-first traversal that are empty, and remove those ...
Andy Dalton's user avatar
  • 13.8k
9 votes
Accepted

sed/awk to remove double quotes " that are inside of curly braces {}

It would be easier with sed: sed -e :1 -e 's/\({[^}]*\)"\([^}]*}\)/\1\2/g; t1' Or perl: perl -pe 's{\{.*?\}}{$& =~ s/"//gr}ge' Note that it assumes there's no nested {...}. To handle nested {....
Stéphane Chazelas's user avatar
8 votes
Accepted

How to check if its safe to delete a library?

You should probably let the package manager of your distribution decide if it's safe to remove it or not. Maybe try to remove it with apt-get remove libgme0 and see if it wants to remove other ...
Bigon's user avatar
  • 2,102
8 votes
Accepted

Why does `find` not delete the files it found?

Juxtaposition binds more tightly than the -o operator. So your command is equivalent to find C:\\path\\to\\Pictures\\pmcctv \( -name cap_*.jpg \) -o \ \( -name ...
Gilles 'SO- stop being evil''s user avatar
8 votes

Can I Delete 70-persistent-net.rules?

You could, but there is probably an even better solution: Either in /lib/udev/rules.d or in /etc/udev/rules.d, there should be a 80-persistent-net-generator.rules file (the number at the beginning ...
telcoM's user avatar
  • 90.6k
8 votes
Accepted

sed or awk: remove numbers after a symbol

Just a slight modification from your sed command: sed 's/^>[0-9]\+[_]/>/g' the s is the sed substitute command, it searches for the string on the left hand side and replaces it with the string ...
jesse_b's user avatar
  • 36.4k
8 votes

How do I delete a directory that contains only '.' and '..', but rm says is not empty?

The directory entries . and .. are an artefact from the early 1970s, when UNIX did run on tiny machines and code needed to be tiny as well. So the people created real hardlinks to directories named . ...
schily's user avatar
  • 18.9k
7 votes

How do I delete a directory with hyphen in the name please?

try: rm -r -- -ltr man rm: To remove a file whose name starts with a '-', for example '-foo', use one of these commands: $ rm -- -foo OR $ rm ./-foo
Oscar Andrea Merandi's user avatar
7 votes

Delete lines with value below (or above) threshold

In awk, you can use something like for (i = 1 ; i <= NF ; i++) { ... } to loop over all fields on a line. With that, something like: awk '{for(i = 1 ; i <= NF ; i++) { if ($i ~ /^[0-9]...
ilkkachu's user avatar
  • 135k
7 votes
Accepted

Delete lines with value below (or above) threshold

perl to the rescue $ cat ip.txt AA 110 B 10 .. BB 15 ... BBB 20 D F 25 D D AA 111 B 50 .. BB 55 ... BBB 30 F F 45 F F AA 112 C 2 .. BB 3 ... BBB 0 ...
Sundeep's user avatar
  • 11.9k
7 votes
Accepted

How to remove the trailing slash from a variable which defines a directory from a time test output

Try this posix parameter expansion : echo "${client%/}" so echo "${client%/} has files older than 2 days ..." Parameter Expansion expands parameters: "$foo", "$1". You can use it to perform ...
Gilles Quénot's user avatar
7 votes
Accepted

Remove a combination of numbers and symbols from a string using the ${VARNAME//pattern/} way

With ksh or bash -O extglob (or after shopt -s extglob in a bash script) or zsh -o kshglob (or after set -o kshglob in a zsh script): VG_MY=${VG_MY//+(=)+([0-9])+(=)/} The +(...) is a ksh extended ...
Stéphane Chazelas's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible