The Stack Overflow podcast is back! Listen to an interview with our new CEO.

New answers tagged

14

Okay, I finally solved the issues. It was due to the filesystem errors that cause ls to display wrongly, and other utilities to malfunction. I'm sorry that the question title is misleading (despite that there are indeed many files inside u/, the directory is not extremely large). I solved the problem by using a live usb since the corrupted filesystem is /. ...


6

You can't remove huge quantities of files using rm. You can either do find u/ -type f -print0 | xargs -r -0 rm -f this will delete only files; to delete everything, use find u/ -print0 | xargs -r -0 rm -rf you can probably use the --delete option of find, if your system has it: find u/ -type f --delete or the funky method with rsync: mkdir emptyfolder ...


2

You can try find /u -type f | while read f; do rm -f $f; done This will take a while but might work. For some reason, loops in bash works well when other approaches fails.


2

Use rm -rf directory instead of rm -rf *. We were initially doing rm -rf * while in the directory to clear the contents and thought that was as fast as it could get. But then one of our senior engineers suggested we avoid using the asterisks (*) and instead pass in the parent directory, like rm -rf directory. After some heavy debate about how that wouldn'...


6

This is an old solution, which means only old guys like me remember it. Find the inode of the file with ls -i. Let's say it is 1611645009. Then in the directory you mention execute the command find $NAME_OF_DIRECTORY -inum 1611645009 -exec /bin/rm {} \; If that doesn't do it, then I'm baffled too.


0

I had a file which showed up via ls, but I couldn't rm it. I was able to mv the file to a new directory. In my specific case to get the file to show up in ls I had to use a wildcard (e.g. ls ./some*.jpeg) and I had to use the same wild card in mv (e.g. mv ./some*.jpeg ./newdir). Also, in my case I wanted to actually keep the file (but it was corrupted). ...


0

It seems you created an ordinary symbolic link and you ran file $DATAPATH without single quotes on the folder and not on the symlink which would have given you something like $ file '$DATAPATH' $DATAPATH: symbolic link to /path/to/xyz You can delete the symlink with rm -i '$DATAPATH' which you will have to confirm with y Enter.


3

I think the most generalizable approach is to build a simple one-liner shell command to send to find: find . -name "*.txt" -exec sh -c 'cat {} >> foobar && rm {}' A side note: Your glob *.txt matched your output file, all.txt. You'll avoid edge cases if the file you're appending too (foobar in my case) doesn't match your -name glob. Just ...


0

Here's how I'd probably do it. for my example, I set up directory /test to use for testing. find /test -type d -iname folder_I_want_to_empty -print0 | xargs -0 -I % find % -type f -print -delete First, I find the directories (-type d) called folder_I_want_to_empty. Then for each of those, I have xargs run a find for files within that directory, print ...


0

You can use rm -fr */folder_I_want_to_empty/* */folder_I_want_to_empty/.??* Please note that this command is very destructive and does not prompt but instead removes non-interactively without mercy. If you wish to see what would be deleted, replace rm -fr by ls -ld: ls -ld */folder_I_want_to_empty/* */folder_I_want_to_empty/.??* Small print: Due to the ...


0

Don't call an external process to check, but use the shell internal function test if test -d "$i"; then # check that directory exists Between the test and the call to rm it is possible other processes already wiped the directory out, so the rm will fail. You can use the switch -f or redirects the errors /bin/rm "$i/*" 2> /dev/null # remove the ...


0

The -f option to rm makes the utility not complain if the file operands do not exist. It would also not exit with a failure. What's possibly happening in your case is that one or several of the directories are empty apart from files or directories with hidden names, so the * at the end of your pattern isn't expanded to anything. You can make the * glob ...


-1

If your goal is to remove all files in an array of folders, another alternative would be to pass in the array in rm like so: #!/bin/bash $IMAGES_DIR="/Users/michael/scripts/imagefiles" $BACKUP_DIR="/Users/michael/scripts/imagebackups" # ... # to preserve whitespaces in path strings, add double quotes around each variable array=( "$IMAGES_DIR" "$...


4

Below I create and then delete a file named "*". The "\" is often called an "escape" by Unix guys as it tells bash to not interpret the next character, use it literally. [ahill@infprd-sftp01 ~]$ ls -l total 0 [ahill@infprd-sftp01 ~]$ touch test1 [ahill@infprd-sftp01 ~]$ touch \* [ahill@infprd-sftp01 ~]$ ls -l total 0 -rw-rw-r-- 1 ahill ahill 0 Sep 20 16:...


Top 50 recent answers are included