Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
0 votes
1 answer
62 views

Remove all files in *that* directory except *these* files - dangerous in remote directory?

I'm writing a small bash script for deploying a bunch of files to a specific remote directory. The process is as follows: Mount remote directory (sshfs) Remove everything except these few files Copy ...
OZ1SEJ's user avatar
  • 417
0 votes
1 answer
77 views

Aliasing the rm command to avoid evaluating separate asterisks

I would like to use the rm command such that it returns an error when there is a separate asterisk in its argument. For example, if I type rm -f * .txt , then this command should not be executed (this ...
user avatar
1 vote
1 answer
2k views

Bash one-liner: check if a directory exists; if so, delete it, then create it anyway

I have to use bash -c, so i need something like bash -c "rm -r tmp/mydir1 && mkdir -p tmp/mydir1/mydir2" but it does not work in this way.
eric_parrot's user avatar
0 votes
1 answer
229 views

delete all files except a pattern list file

I need to delete all the files in the current directory except a list of patterns that are described in a whitelist file (delete_whitelist.txt) like this: (.*)dir1(/)? (.*)dir2(/)? (.*)dir2/ser1(/)?(.*...
ronen's user avatar
  • 1,490
1 vote
1 answer
1k views

How do you remove an entire directory except for certain subdirectories in linux? [duplicate]

Suppose I run the following command in linux: $ mkdir -p mp3 jpeg/dir1 jpeg/dir2 txt $ touch mp3/1.mp3 mp3/2.mp3 mp3/3.mp3 $ touch jpeg/1.jpeg jpeg/2.jpeg jpeg/3.jpeg $ touch txt/1.txt txt/2.txt txt/3....
user1068636's user avatar
  • 1,939
1 vote
4 answers
3k views

Recover deleted files from local git repo?

I accidentally rm'd some files in a git repo. Those files had been changed a lot and hadn't been git add'd yet. Is there a way to restore the files to their 'last saved' state? UPDATE Just wanted to ...
Aaron Parisi's user avatar
0 votes
0 answers
53 views

Bash script to delete certain files from "semi-sequentially" numbered directories?

I have directories that are sequentially numbered 1001, 1002, 1003, etc... Some directories may not exist so we may see instances like 1001, 1002, 1003, 1007, 1008, etc... and I want to delete all ...
jmt76's user avatar
  • 1
11 votes
3 answers
66k views

Automatically solve rm cannot remove path : device or resource busy error

I am trying to remove a directory /path/to/dir using the rm -rf command. Unfortunately I get the error rm: cannot remove '/path/to/dir/.nfsdda293a660f276ca0000000a': Device or resource busy After a ...
JejeBelfort's user avatar
  • 1,663
0 votes
1 answer
256 views

bash: removing many files of the same extension [duplicate]

I am dealing with the folder consisted of many log filles. I have to remove all of them using bash. My version using rm does not work: rm "${results}"/*.log that gives me: ./dolche_vita.sh:...
James Starlight's user avatar
0 votes
2 answers
1k views

Cannot remove .git/: Directory not empty

I have a .git/ folder that I'd like to delete. However, it isn't possible because there is a puzzling file into it which is not reachable. Below is my try: $ rm -rf .git/ rm: cannot remove '.git/': ...
TVG's user avatar
  • 422
0 votes
0 answers
310 views

Not able to remove a file

Am not able to delete a file called 'go' in my bash home directory. I tried rm -r go..it dint work.it showed permission denied.as am a beginner I really can't figure it out how such a directory was ...
User's user avatar
  • 1
0 votes
1 answer
2k views

Deleting certain files in multiple folders in one directory in Linux

I have about 30+ folders I want to delete only certain file types inside. I can do this by using: Find Dir1, Dir2, Dir3 (Etc.) -name "*.filetype" -Delete However, this would be very long to ...
ADAS's user avatar
  • 1
-1 votes
3 answers
992 views

How do you delete all hidden and non-hidden files except one?

How do you delete all hidden and non-hidden files except one in Bash? Because I am creating a repository, and just now creating an update script.
anon824303's user avatar
0 votes
1 answer
113 views

find outputs result but -exec ignores it

I wrote a simple command to find folders older than a month and delete them. Here is the command : find . -type d -mtime +31 -exec bash -c 'rm -rfv "$0"' {} \; It works fine in most cases, ...
Airmoi's user avatar
  • 35
3 votes
1 answer
6k views

How to remove all files except the ones that matches a pattern along several number of directories

lets say I have a my_dirs/ directory, insdie that directory I have several parallel subdirectories which has several files and I want to delete all of them except the ones that have the substring '....
Valentin's user avatar
  • 431
0 votes
1 answer
108 views

Operating on multiple specific folders at once with cp and rm commands

I'm new to linux (using bash) and I wanted to ask about something that I do often while I work, I'll give two examples. Deleting multiple specific folders inside a certain directory. Copying multiple ...
Argento's user avatar
0 votes
3 answers
2k views

Command to delete multiple files

I'm a new student in programming and I'm stuck on a question, which is: Enter a command to delete all files that have filename starting with logtest, except logtest itself (delete all files starting ...
TONY FRANK C's user avatar
0 votes
2 answers
62 views

How to delete a folder by parameter and protect by user bugs/errors

Let's say I have in script something as following which get script input and delete that folder: cd ` dirname "$0" ` X="${1}" rm -rf ${X}/* I see few bugs in this code: if the ...
Nir's user avatar
  • 2,629
-2 votes
2 answers
4k views

How to "rm -rf" with excluding files and folders with the "find -o" command

I'm trying to use the find command, but still can't figure out how to pipe the find ... to rm -rf Here is the directory tree for testing: /path/to/directory /path/to/directory/file1_or_dir1_to_exclude ...
stackbiz's user avatar
  • 1,398
-2 votes
2 answers
451 views

Delete Everything in a Directory Except One FIle - Using SSH

In BASH, the following command removes everything in a directory except one file: rm -rf !(filename.txt) However, in SSH the same command changes nothing in the directory and it returns the following ...
Thomas's user avatar
  • 117
0 votes
3 answers
167 views

How to delete all but the oldest duplicate flle listed in a text file using rm

I have followed an article by Rick James on how to locate duplicates. He never went farther than producing a list of files that contain their md5 hash, count, file name. find . -type f -not -empty -...
John's user avatar
  • 486
1 vote
1 answer
308 views

How to prevent rm from reporting that a file was not found in CURL?

When executing the command curl -k HOST --user user:pass -Q "rm nosuch" I get an answer curl: (21) rm command failed: No such file or directory I tried using -f, but the result hasn't ...
Григорий's user avatar
0 votes
2 answers
948 views

rm -r command not working inside of shell in macos catalina

I am new to shell scripting. I wrote basic simple.sh file on mac. I put my .sh file to /usr/local/bin directory and I added sh file to the chrometab so as to run every minute. My sh file works fine on ...
Attila Türkkan's user avatar
1 vote
3 answers
703 views

How to use rm interactively in a while read loop?

I'm little stuck in a script that's used to find and delete certain files. I want to give the user the opportunity to iterate through the list and delete each file only after human approval. However, ...
rizzer's user avatar
  • 13
0 votes
1 answer
129 views

Batch removal (rm -rf) just for existing directories in a subdirectory

Background: I have written a very simple backup removal script which should delete backups that are older than three version updates. This works by always adding the newly installed version of my ...
Peleke's user avatar
  • 961
9 votes
3 answers
31k views

Cannot remove .git: Directory not empty

Unable to remove .git I have a git repository, Rift, that I am trying to remove. On attempting to run rm -rf, I get the error: rm: cannot remove 'Rift/.git/objects/pack': Directory not empty. When I ...
Thoth's user avatar
  • 2,246
1 vote
3 answers
4k views

Answer no to all questions using the yes command

I'm trying to remove all files except read-only ones, but this command removes all of them anyway: yes n | rm * Did I do something wrong? If not, why doesn't it work?
alek3y's user avatar
  • 29
0 votes
1 answer
120 views

Removing a remote file through ssh stops the while loop the command is in?

I have this script that lists files having been modified more than 10minutes ago on a remote disk, retrieve them locally, and then, is supposed to delete them on remote. The script works when I don't ...
pierre_j's user avatar
  • 949
0 votes
1 answer
522 views

xargs rm -r with regex returns "no such directory"

I have a folder with several subdirectories b1_3, b1_4 etc. Inside these subdirectories I have some files and other subdirectories, and the names of these subdirs follow a pattern alpha[digits]_etc, ...
Lfppfs's user avatar
  • 124
2 votes
3 answers
12k views

How to make rm not delete symbolic links?

So, I was getting rid of the unwanted files and I deleted some folder. After some time I saw another folder which appears red in ls -la. I knew I deleted the actual folder and thus I was very sad and ...
Machine Yadav's user avatar
0 votes
0 answers
22 views

bash: command works when inputted manually but not on script [duplicate]

I have a simple bash command, which does some stuff and at some point tried to delete everything except a few things. When running inside a script, it fails. But when I try to run it again, it works. ...
blasrodri's user avatar
  • 446
1 vote
1 answer
178 views

Bash SFTP batch file equivalent for Amazon S3

I need to execute a series of remove file commands (listed below) to an Amazon S3 bucket from a pipe. But I need to execute them line by line using the Command Line Utility (v2) from Amazon S3 and I ...
nwood21's user avatar
  • 312
0 votes
1 answer
176 views

find command: delete everything but one folder

I have this command: find ~/Desktop/testrm -mindepth 1 -path ~/Desktop/testrm/.snapshot -o -mtime +2 -prune -exec rm -rf {} + I want it to work as is, but it must avoid to remove a specific ...
JoulinRouge's user avatar
1 vote
4 answers
73 views

Remove all files contain specific string - Bash

I have these bad data AWS-Console.pngcrop-AWS-Console.png Alimofire.pngcrop-Alimofire.png ...
code-8's user avatar
  • 58.4k
0 votes
0 answers
41 views

Why do I get prompted when I attempt to delete my project?

I just did rm -r projectName then I got: override r--r--r-- honey/staff for projectFolder//.git/objects/pack/pack-9323e9c7e9a036b5f0f9186d05e31536e342308.pack? Based on a Google search, I know I ...
mfaani's user avatar
  • 36.1k
0 votes
1 answer
2k views

Why script delete file even with message rm: cannot remove '/path/file.txt': No such file or directory?

I made a bash script that delete files into a folder through a loop. The shell give me the error rm: cannot remove '/path/file.txt': No such file or directory but the script remove the files ...
onehalf's user avatar
  • 67
1 vote
0 answers
162 views

MacOS Bash find and echo not working as expected?

I was working on housekeeping an application volume and as a rule of thumb I prepend an echo before the rm in my find command. This has worked for me a very long time until today. I'm just wondering ...
meappy's user avatar
  • 87
0 votes
6 answers
1k views

Script to delete old files and leave the newest one in a directory in Linux

I have a backup tool that takes database backup daily and stores them with the following format: *_DATE_*.*.sql.gz with DATE being in YYYY-MM-DD format. How could I delete old files (by comparing ...
user avatar
0 votes
0 answers
72 views

Why isn't pattern matching working with 'rm' from bash script? [duplicate]

I am cleaning up temporary files throughout each step of a long bash script. I am hoping to remove, using absolute file paths, every file with a certain prefix that is not a log file. I have tested ...
jremedy's user avatar
  • 137
2 votes
1 answer
10k views

Unable to delete all files from directory [closed]

I want to delete all the files under the volume directory. The directory is inside the Kubernetes pod. So I am using the exec command. My command - kubectl exec $POD -- rm -rf /usr/local/my-app/...
lucy's user avatar
  • 4,486
2 votes
2 answers
802 views

Check user input on "rm -i"?

Is there a way to check the user's response to a rm -i execution? I'd like to echo something depending on whether or not the user responded with y or n. The command returns successfully regardless ...
genbatro's user avatar
  • 166
-2 votes
3 answers
494 views

How do I delete a file with name ''$'\r'?

For some reasons, I have a file in one of my directories that I cannot delete: $ ls -al drwxrwxr-x 2 xxx xxx 4096 Oct 10 14:05 ''$'\r' (more files follow...) The problem is that I cannot delete it. ...
user52366's user avatar
  • 1,125
-1 votes
1 answer
498 views

Bash script rm command failed in while loop

I have a remote server with a couple of cache keys and I am trying to delete them. However, I am getting an error when I am running the script. This is the script that I am using at the moment. Also, ...
andregr_jp's user avatar
2 votes
2 answers
2k views

Cannot rm -rf using variable

I am trying to write a BASH script which will delete recursively all files in a directory. When I write in console rm -rf /home/dir/dir/*, it works great, but I can't do it using variables. VAR="/...
Laughing_Man's user avatar
4 votes
4 answers
6k views

Is there any difference between rm -r and rm -R?

Title is Description. I just wonder there is any difference between rm -r and rm -R. Linux man description is seemed like they are totally same(and one more, --recursive), but many people use -r ...
Aiden Yeomin Nam's user avatar
3 votes
1 answer
7k views

Cannot remove symbolic link to directory

I have a directory reference in my Downloads directory that contains a symbolic link (created with ln -s) to another directory. I get conflicting error message when trying to remove the symlink: rm ...
Tom Kelly ケリー・トム's user avatar
0 votes
1 answer
117 views

Deleting files and prompting user for his decision

printf "$(find "./RIT/"${neki_array["letnik"]}"" -name "*" )\n">> tmpfile while IFS='' read -r line || [[ -n "$line" ]] ; do echo "Do you want to delete "$line"? y|Y or n|N" read ...
AverageProgrammer's user avatar
15 votes
5 answers
28k views

Trying to remove my .git folder and 'rm -r .git --force' is not working

rm -r .git rm -r .git --force I get the following and there seems to be a never ending supply after I enter 'yes' and move to the next. override r--r--r-- redacted/staff for .git/objects/95/...
seamus's user avatar
  • 2,851
-1 votes
1 answer
120 views

Remove ".bash_aliases" with bash script

In my .bashrc I'm using .sh script for easily configuring newly installed Debian. But while trying to rm -f ~/.bash_aliases wget https://raw.githubusercontent.com/.../.bash_aliases rm -f ~/.bashrc ...
Shanduur's user avatar
2 votes
1 answer
3k views

How to deal with missing operand error when trying to rm

I have the following code rm "$torrent"/*.{txt,nfo,jpg} && echo "removed \"txt,nfo,jpg\" files" If there are no files to delete then I get the "missing operand" error which I would like to ...
user avatar