How can I delete contents in a folder using a bash script?
You should say "... my bin folder", not "my /bin folder". /bin
is an absolute path, bin
is a relative path.
rm -rf ~/bin
removes $HOME/bin
, so not what you want either.
Now, it depends on where you are: if you are in your project directory when you type the command, just type rm -rf bin
.
Shell Script for Delete all files in directory and print count of deleted files
The problem is that, by default, rm
does not output anything, so there's nothing for wc
to count.
You could use the -v
option for rm (check your man page)
rm -vrf pattern | wc -l
# or
rm -vrf pattern | echo "$(wc -l) files deleted"
Another technique is to use find
find * -depth -print -delete | wc -l
Using the -depth
directive so that a directory's files are removed before the directory itself. Demo:
$ tree
.
├── a
├── b
├── c
├── d
│ ├── g
│ ├── h
│ ├── i
│ └── k
│ ├── l
│ └── m
├── e
│ └── j
└── f
4 directories, 9 files
$ find * -depth -print -delete | wc -l
13
$ tree
.
0 directories, 0 files
I want to delete all the files under a directory using shell script, passing file directory as an argument
Use the find command for this:
find "$1" -maxdepth 1 -type f -delete
Pass the directory as a parameter in the scripts ($1) and then ensure you are only searching the directory at one level with -maxdepth 1 and searching for files only with -f. Delete what ever is found with -delete.
Deleting a directory contents using shell scripts
You are modifying PATH
variable, which is used by the OS defines the path to find the utilities (so that you can invoke it without having to type the full path to the binary). The system cannot find rm
and fuser
in the folders currently specified by PATH
(since you overwritten it with the directory to be deleted), so it prints the error.
tl;dr DO NOT use PATH
as your own variable name.
How to safely delete the contents of a folder with a bash script?
_buildFolderTag
is fixed in your script, so it should not go out of build
subfolder, ever.
You may check what is .
currently. It prevents deleting my_crucial_dir/build
instead of my_daily_build/build
.
You can use trash-cli instead of rm
.
On Ubuntu, it is in the repo: sudo apt-get install trash-cli
throw files to trash: trash-put file1
Once, you should empty the trash: trash-empty
If it is a production script, the user would not be happy with something gone to trash. But you can echo the full path to the user and ask for confirmation:
answer=""
echo "Confirm deleting the build folder: "$(pwd)${_buildFolderTag}
while [[ "$answer" != "y" && "$answer" != "n" ]]
do
read -p "Your choice (y/n): " answer
done
if [[ "$answer" == "y" ]]
then
rm -rf ./${_buildFolderTag}/*
fi
Remove the content of multiple folders with a bash script in Moba
You can give multiple files/directories in rm command:
rm -rf path1 path2 path3 pathN
Please note, if you use -rf
option, it will delete everything under the path.
Remove specific file from directory using shell script
You are mixing in Windows CMD syntax. The proper sh
script would look like
OLD_DIR=$(pwd)
echo "$REPORT_HOME"
REPORT_HOME="$REPORT_HOME/REPORT_HOME"
cd "$REPORT_HOME/lib"
if [ -f antlr-2.7.7.jar ]; then
rm -rf "antlr-2.7.7.jar"
cd "$OLD_DIR"
However, this is humongously overcomplicated. To delete the file, just
rm -f "$REPORT_HOME/REPORT_HOME/lib/antlr-2.7.7.jar"
The -f
flag says to just proceed without an error if the file doesn't exist (and also proceed without asking if there are permissions which would otherwise cause rm
to prompt for a confirmation).
Perhaps see also What exactly is current working directory?
This assumes that the variable REPORT_DIR
isn't empty, and doesn't start with a dash. For maximal robustness, try
rm -f -- "${REPORT_HOME?variable must be set}/REPORT_HOME/lib/antlr-2.7.7.jar"
Linux, shell script] How to dynamically delete folders by searching by folder name
Here are some guidelines for you:
- Get the various date segments using GNU date
- Example:
current_year=$(date +%Y)
will give you the current year
- Example:
- Use this type of code to loop over the directories, one level at a time
- Example:
for current_dir in /data/*/; do
...
- Example:
- Get just the directory name for each item (strip off slashes) using basename or string modification
- Example:
current_dir=$(basename "$current_dir")
- Example:
- At each level, check if the number is lower than the current year/month/day (depending on level)
- Compare using -lt / -gt
- Example:
if [ "$current_dir" -lt "$current_year" ]; then
... (remove it - or do some logging to start with to make sure you're on track)
- If the number is equal (-eq) to the current year/month - then you can loop through that next
- Example:
for current_dir2 in /data/"$current_dir"/*/; do
...
- Example:
Deleting content of folder with shell script
Glob expansion doesn't happen inside quotes.
Try:
rm -r -- "$DIR"*
(Just make really sure you don't put a space after the quotes.)
Related Topics
How to Test My Bash Script on Older Versions of Bash
How to Stop Sed from Buffering
How to Setup Cron Job on Amazon Linux Ami
How to Print $ in Shell Script
Simulate Effect of Select() and Poll() in Kernel Socket Programming
Docker Create Two Bridges That Corrupts My Internet Access
Error While Installing Mongodb on Ubuntu 16.04
How to Delete All Files Starting with ._ from The Shell in Linux
How to Write Kernel Space Memory (Physical Address) to a File Using O_Direct
How to Redirect Entire Output of Spark-Submit to a File
Efficiently Read The Average Color of The Screen Content Rendered by Xbmc
Cmake Error: The Following Variables Are Used in This Project, But They Are Set to Notfound
Ldd Shows Varied Addresses on X86 Linux
Tomcat Server Creating Directories in Tmp
Setup Sftp to Use Public-Key Authentication
What Is The Maximum Number of Subdirectories Allowed in Ext4