On Windows I am trying to add a Makefile target to remove all files from a specific directory (NOT including subdirectories)...
clean_files:
rm -f Build/*.*
But I get the error: /bin/sh: rm: command not found
Running it from the command line works and running it without the *'s works.
clean_files:
- rm -f Build/*
putting a '-' before a make command will ignore any errors from that command, like
rm: cannot remove `Build/subdir': Is a directory
For removing all files from directory (NOT including sub-directories) consider:
clean_files:
find Build/ -type f -maxdepth 1 -delete
Related
How to remove a file(s) with strange name in ubuntu?
I used ssh-keygen, I think I copied the command line with the linebreak at the end and created two files with a very strange file name.
https://ibb.co/0mC5fMj
Try to delete
rm \'\'$\'r\'
But result:
rm: cannot remove ''\'''\''$'\''r'\''': No such file or directory
Try to add -- at the beginning of the file name.
$ rm -v -- #file
$ rm -v -- "#file"
Try to add ./ at the beginning of the file name.
$ rm -v ./#file
If the previous tips do not work, you can still remove it using the inode number with:
ls -li
output:
5133242 -rw-r--r-- 1 user #*%/file
then using find
$ find . -inum 5133242 -delete
I'm trying to exclude the current directory from the tarball without excluding its contents, because when I extract it out using the -k flag I get an exit status of 1 and a message
./: Already exists
tar: Error exit delayed from previous errors.
How do I do this? I've tried the --exclude flag but that excludes the contents also (rightly so). I'm trying to code this for both the OSX/BSD and GNU versions of tar.
Test case:
# Setup
mkdir /tmp/stackoverflow
cd /tmp/stackoverflow
mkdir dir
touch dir/file
# Create
tar cCf dir dir.tar .
# List contents
tar tf dir.tar
gives
./
./file
showing that the current directory ./ is in the tar. This would be fine, but when I do the following:
mkdir dir2
tar xkfC dir.tar dir2
due to the -k flag, I get an exit code of 1 and the message
./: Already exists
tar: Error exit delayed from previous errors.
To exclude the current directory you can create your archive on this way:
tar cf /path/to/dir.tar ./*
use ./*instead of ., this will not match current directory (.) and therefore not include in the archive
This does the trick:
GLOBIGNORE=".:.."
cd dir
tar cf ../dir.tar *
The extra cd is to replace the use of the -C flag, so that we can use a glob. The GLOBIGNORE ignores the current directory, but also sets shopt -s dotglob implicitly to include hidden files. Using * instead of ./* means that the tar file listing doesn't list ./file, but instead lists it as file. The entire listing of the tar is:
file
By using the command :
rm /file_path/*.csv
I can delete all csv files in the required folder.However if the directory is empty or there are no csv files I get the following error:
No such file or directory
How do I avoid this error?I have this logic in a script with certain downstream dependancies so throwing this error will cause the rest of my code to stop.Whats the best way in bash to delete files only if they exist in the directory?
Another variant is to check if your folder is empty before to run your script:
find file_path/ -type d -empty
It returns the name of your folder if it is empty.
Or use the "-f" option with rm command if you want only avoid the error message:
Without:
rm -r file_path/*.csv
rm: cannot remove ‘file_path/*.csv’: No such file or directory
With:
rm -rf file_path/*.csv
See Test whether a glob has any matches in bash for ways to check if /file_path/*.csv matches anything. However, even if you do such a test before running the rm command it may fail if the directory has a very large number of CSV files. See Argument list too long error for rm, cp, mv commands.
If you have a modern version of find, this is a reliable and efficient option:
find /file_path -maxdepth 1 -type f -name '*.csv' -delete
You can do: [ -f /file_path/*.csv ] && rm /file_path/*.csv
I'm trying to all subdirectories and its contents with the name "bin". I've used the find command with option -delete to perform the delete and it has worked to some degree (JFX folders begin at 01 and I've managed to delete "bin" folders up to JFX13). However, not all "bin" folders and its contents are deleted, which makes me very puzzled.
I would appreciate if anyone can figure out what I'm not doing correctly.
Here's how it looks right now
According to the man page of find, -delete is for deleting files also folders but only if there are no files inside. So instead of first deleting the files and then the folders again, I would suggest to use -exec with rm -rf to clean up altogether in one-shot.
find . -name "bin" -type d -exec rm -rf "{}" +
The actual information from te man find page,
-delete
Delete files; true if removal succeeded. If the removal
failed, an error message is issued. If -delete fails, find's
exit status will be nonzero (when it eventually exits). Use
of -delete automatically turns on the `-depth' option.
This calls rm -rf on the all the folders returned from find command in one-shot rather than involing one rm command for every folder found.
Note:- Use rm -rf carefully. Run this only if you are absolutely sure of the folders you want to delete, avoid running it otherwise.
I'm using the command cp ./* "backup_$timestamp" in a bash script to backup all files in directory into a backup folder in a subdirectory. This works fine, but the script keeps outputting warning messages:
cp: omitting directory `./backup_1364935268'
How do I tell cp to shut up without silencing any other warnings that I might want to know about?
The solution that works for me is the following:
find -maxdepth 1 -type f -exec cp {} backup_1364935268/ \;
It copies all (including these starting with a dot) files from the current directory, does not touch directories and does not complain about it.
Probably you want to use cp -r in that script. That would copy the source recursively including directories. Directories will get copied and the messages will disappear.
If you don't want to copy directories you can do the following:
redirect stderr to stdout using 2>&1
pipe the output to grep -v
script 2>&1 | grep -v 'omitting directory'
quote from grep man page:
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
When copying a directory, make sure you use -R
cp -R source source_duplicate_copy_name
-R, -r, --recursive copy directories recursively
--reflink[=WHEN] control clone/CoW copies. See below
--remove-destination remove each existing destination file before
attempting to open it (contrast with --force)
--sparse=WHEN control creation of sparse files. See below
--strip-trailing-slashes remove any trailing slashes from each SOURCE