Remove if exist linux

Linux filename check if exist and delete

I have some problem in Shell scripting. So I have to write a script that find every file in a directory with this string: «gom». So i found all of them. After I need to cut it off, and compare that the remaining filename is exist. If exist i need to remove the file that contains the string. Example: there are 5 files: algomb, gomba, alb, algomba, alba. I need to find the filenames with «gom». algomb, gomba, algomba. After it i need to cut the «gom». And a remaining filenames is exist I need to remove the file with «gom» string. So after the cutting «gom» there will be 5 files: alb, ba, alb, alba, alba So there are two files that is extist: alb, alba. I need to remove the following files: algomb, albomba. After it the will be 3 files: gomba, alb, alba. Sorry for my bad english. I can find, I can remove, but I cant compare the filenames. Here’s my code:

#!/bin/bash sz="gom" talal=`find . -type f -name "*$sz*" -exec basename <> \;` ossz=`find . -type f -exec basename <> \;` c=`echo $$` for c in ossz; do if [ ! -d ]; then echo "This is a directory" else if [ -f ]; then find .-type f -name "*$sz*" -exec basename <> \; else echo $$ fi fi done 

So this is works. This echo $$ is give back the filename without «gom». But I cant compare these values with find . -type f -exec basename <> \; results. Sorry for my bad english. Can sombody help me? Best regards, Richard

2 Answers 2

I would do it this way, without find .

shopt -s globstar for gom_file in **/*gom*; do # Skip non-regular files [[ -f $gom_file ]] || continue # Delete the file if the gom-free file exists [[ -f $ ]] && rm "$gom_file" done 

Using find is slightly less efficient, since you need to fork a new shell for each file:

find . -type f -name "*gom*" -exec bash -c 'echo rm -f "$"' <> \; 

Run this to test that it outputs the rm commands you want to execute, then run it again with echo removed.

Читайте также:  Файловая система linux преимущества

Источник

How to delete a file in bash

Any file can be deleted temporarily and permanently in bash. When a file is removed temporarily by using a graphical user interface, then it is stored in the Trash folder, and it can be restored if required. The file which is removed permanently cannot be restored later normally. `rm` command is used to remove the file permanently from the computer. If any file is removed accidentally by this command, then it can be restored from the backup. How any file can be removed from the terminal and the graphical user interface are shown in this article.

Delete the file using `rm` command:

`rm` command can be used with option and without the option for the different types of delete. The syntax of the `rm` command is given below.

Syntax:

‘-i’ option can be used with `rm` command to provide a prompt before deleting any file to prevent accidental deletion. ‘-f’ option can be used with `rm` command to remove any file forcefully. The different uses of the `rm` command are shown below.

Example-1: Delete the file using `rm` command without the option

You can apply the ‘rm’ command to remove an existing file. In the following script, an empty file is created by using the ‘touch’ command to test ‘rm‘ command. Next, ‘rm’ command is used to remove the file, test.txt.

# Set the filename
filename = ‘test.txt’
# Create an empty file
touch $filename
# Check the file is exists or not
if [ -f $filename ] ; then
rm test.txt
echo » $filename is removed»
fi

Example-2: Delete the file using `rm` command with -i option

The following script will ask for permission from the user before removing the file for ‘-i’ option. Here, the filename will be taken from the user as input. If the file exists and the user press ‘n’ then the file will not remove otherwise the file will remove.

# Take the filename
read -p ‘Enter the filename to delete: ‘ filename

# Check the file is exists or not
if [ -f $filename ] ; then
# Remove the file with permission
rm -i » $filename »
# Check the file is removed or not
if [ -f $filename ] ; then
echo » $filename is not removed»
else
echo » $filename is removed»
fi
else
echo «File does not exist»
fi

Example-3: Delete the file using `rm` command with -v option

The following script will take the filename by a command-line argument. If the file exists then, it will print a remove message with the filename for ‘-v’ option.

Читайте также:  Root без пароля linux

# Check the file is exists or not
if [ [ $1 ! = «» && -f $1 ] ] ; then
# Print remove message
rm -v $1
else
echo «Filename is not provided or filename does not exist»
fi

Example-4: Delete multiple files using `rm` command

More than one file can be deleted by using ‘rm’ command and separating the filenames with space. In the following script, multiple filenames will be taken from the command line arguments. If any file does not exist, then it will show a message otherwise filenames will be combined by the space and stored into the variable named ‘files’. Next, the rm command will be executed with the ‘files’ variable to remove multiple files.

# Check the multiple filenames are given or not
if [ $# > 2 ] ; then
# Reading argument values using loop
for argval in «$@»
do
if [ -f $argval ] ; then
files+= $argval $space
else
echo » $argval does not exist»
fi
done

# Remove files
rm $files
echo «files are removed.»
else
echo «Filenames are not provided, or filename does not exist»
fi

Conclusion:

The above examples show the different types of ways to delete the file using a bash script to help bash users to do this type of task easily.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

Delete folder if it exists [closed]

Questions describing a problem that can’t be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers.

if [ ! -d folder ]; then rm -rf folder; fi 

Given the seeming mistake understanding ! -d , you may want to call rm —preserve-root -rf folder in case folder somehow becomes «

just wondering why it’s needed to check if the folder exists. a simple rm -rf without check should be enough, because if it doesnot exist, it cannot be removed. or do i miss spmething?

nice and easy question, specially useful for CI where a rm -rf on a non-existing folder could return a failure instead of a 0.

1 Answer 1

The if [ ! -d folder ] part is wrong. It’s false on both empty and non empty directories. The exclamation mark is the logical not operator: you’re checking if the directory does not exist before you delete it.

Читайте также:  Вернуть процесс из фона linux

Remove that exclamation mark.

Hot Network Questions

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

check if directory exists and delete in one command unix

Is it possible to check if a directory exists and delete if it does,in Unix using a single command? I have situation where I use ANT ‘sshexec’ task where I can run only a single command in the remote machine. And I need to check if directory exists and delete it.

@Ferruccio- using rm -rf /dir_name does not throw a error but rm -rf /dir_name throws says No such file or directory found. ( Just tested)

5 Answers 5

Why not just use rm -rf /some/dir ? That will remove the directory if it’s present, otherwise do nothing. Unlike rm -r /some/dir this flavor of the command won’t crash if the folder doesn’t exist.

I think the original question was intended to mean: perform the delete only when file/directory exists or not. This command may work and produce similar results but actually doing a test before the command makes more sense.

As stated above this isn’t the answer. One reason for testing before deleting would be inside a Jenkins job. If the directory doesn’t exist and you try to delete it, that will fail the job. Checking beforehand is the better option.

I think this is the correct answer. it does what @remo needs, i.e. deleting a directory only if it exists and not giving an error if it doesn’t. Maybe the question is not quite well put because why would you need to check the existence when this command won’t care?!

Assuming $WORKING_DIR is set to the directory. this one-liner should do it:

if [ -d "$WORKING_DIR" ]; then rm -Rf $WORKING_DIR; fi 

(otherwise just replace with your directory)

bash -c '[ -d my_mystery_dirname ] && run_this_command' 

This will work if you can run bash on the remote machine.

In bash, [ -d something ] checks if there is directory called ‘something’, returning a success code if it exists and is a directory. Chaining commands with && runs the second command only if the first one succeeded. So [ -d somedir ] && command runs the command only if the directory exists.

Источник

Оцените статью
Adblock
detector