Linux remove all *.sln file exclude one file [duplicate] - linux

This question already has answers here:
Bash script to remove all files and directories except specific ones
(5 answers)
Closed 6 months ago.
Can I know how to remove all *.sln file but exclude "work.sln" in the same folder
I try run rm *.sln !("work.sln")
it return /bin/bash: eval: line 128: syntax error near unexpected token ('`
Thank you

Assuming you are already inside the folder that contains the files you need, this is one way to do it (also assuming you are looking for a one-line version, given your example)
for file in $(ls);do if [[ ! $file == work.sln ]]; then rm $file; fi; done
EDIT: as #tripleee pointed out, the ls can be avoided and re-written like this
for file in *; do if [[ ! $file == work.sln ]]; then rm $file; fi; done

Related

I am Attempting to use the output of ls command as an array. And I want it to be line by line [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I need to find the encrypted(.enc) files from a folder and decrypt them.
I will find the .enc files
if [[ -n "$(ls -A /sodaman/tempPrabhu/temp/*.enc 2>/dev/null)" ]]; then
And I used enc_files=($( ls *.enc )) but it takes all the files as one and fails. It considers all the files for the output as one line. So I replaced it with mapfile to decrypt the files one by one but it throws an error
test.sh: line 31: syntax error near unexpected token `<' test.sh: line 31: ` mapfile -t enc_files < <(ls *.enc)'
Below is the script:
if [[ -n "$(ls -A /sodaman/tempPrabhu/temp/*.enc 2>/dev/null)" ]]; then
#create array of encrypted files
mapfile -t enc_files < <(ls *.enc)
enc_files=($( ls *.enc ))
#echo Creating array $enc_files
#decrypt all encrypted files.
echo Creating loop for encryped files
for m in "${enc_files[#]}"
do
d=$(echo "$m" | cut -f 1 -d '.')
echo $d
d=$d.dat
echo $d
/empty/extproc/hfencrypt_plus $m $d Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
echo /empty/extproc/hfencrypt_plus $m $d Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
if [[ -f "$d" ]]; then
mv $m /empty/sodaman/tempPrabhu/blr_temp
#echo Moving file to encrypted archive : mv "$m" /empty/sodaman/enc_archive
echo removing log file : rm log.log
rm log.log
else
echo File was not decrypted successfully
fi
done
fi
Here's a refactoring which avoids several of the http://shellcheck.net/ violations in your attempt.
for file in /sodaman/tempPrabhu/temp/*.enc; do
# avoid nullglob
test -e "$file" || continue
# prefer parameter expansion
d=${file%%.*}.dat
if /empty/extproc/hfencrypt_plus "$file" "$d" Decrypt /empty/extproc/hfsymmetrickey.dat log.log infa91punv
then
# assume hfencrypt sets exit code
mv "$file" /empty/sodaman/tempPrabhu/blr_temp
else
# print diagnostics to stderr
# mention which file failed
# mention which script emitted the warning
echo "$0: $file was not decrypted successfully" >&2
sed "s%^%log.log: $file: %" log.log >&2
fi
rm -f log.log
done
This assumes that you wanted to loop over the files in the directory you examine at the beginning of your script, not in the current directory (perhaps see also What exactly is current working directory?) and that the encryption utility sets its exit code to nonzero if encryption failed (perhaps see also Why is testing “$?” to see if a command succeeded or not, an anti-pattern? which discusses idioms around conditions involving errors). I added a sed command to include the output from log.log in the diagnostics after a failure, though perhaps you would like a different error-handling strategy (exit immediately and let the user troubleshoot? Or rename the log file to a unique name and keep it around for later?)
Don't parse ls results, but use this:
find /sodaman/tempPrabhu/temp/ -maxdepth 1 -name "*.enc"

How can I modify script to print information about all files in directory [duplicate]

This question already has answers here:
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 3 years ago.
How can I finish the script?
Linux version 3.10.0-957.21.3.el7.x86_64 .(Red Hat 4.8.5-36)
#!/bin/bash
echo "Enter the file name"
read x
if [ -f $x ]
then
echo "This is a regular file"
else
echo "This is a directory"
fi
Need modify script which will output all files and directory in /etc/ directory and indicate which one is what (e.g.:
dir1 is a directory
fileA is a file
dir2 is a directory
2nd part of the job I did. need help with
Use a for loop instead of getting the filenames from the user.
#!/bin/bash
for file in /etc/*; do
if [ -f "$file" ]
then
echo "$file is a regular file"
elif [ -d "$file" ]
then
echo "$file is a directory"
else
echo "$file is something else"
fi
done
Don't forget to quote variables, in case the value contains a space. And there are other possibilities than just files and directories.

Moving multiple files in directory that have duplicate file names [duplicate]

This question already has answers here:
Moving multiple files in directory that might have duplicate file names
(2 answers)
Closed 9 years ago.
can anyone help me with this?
I am trying to copy images from my USB to an archive on my computer, I have decided to make a BASH script to make this job easier. I want to copy files(ie IMG_0101.JPG) and if there is already a file with that name in the archive (Which there will be as I wipe my camera everytime I use it) the file should be named IMG_0101.JPG.JPG so that I don't lose the file.
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Usage: $0 image_path archive_path"
exit 999
fi
if [ -d "$1" ] #Checks if archive directory exists
then
echo Image Source directory FOUND
else
echo ERROR: Image Source directory has NOT BEEN FOUND
fi
if [ -d "$2" ]
then
echo Photo Archive FOUND
else
echo Creating directory
mkdir "$2"
fi
if [ find $1 -name "IMG_[0-9][0-9][0-9][0-9].JPG" ] #added this in to be more specific 1/4
then #2/4
for file in "$1"/*
do
dupefile= "$2"/"$file"
while [ -e "$newfile" ];
do
newfile=$newfile.JPG
done
mv "$file" "$newfile"
done
else #3/4
#do nothing
fi #4/4 took all the /4 out, but it's saying theres no such file or directory, even though I've tested it and it says there is.
unexpected token fi is the error I'm getting but the if statement needs to be in there so the specific files i need, are getting moved.
You cannot have an empty else. Take out the else keyword if you don't have an else part.
Also, you should get rid of the superfluous find. Just loop over the files you actually want instead.
for file in "$1"/IMG_[0-9][0-9][0-9][0-9].JPG
do
newfile= "$2"/"$file"
while [ -e "$newfile" ];
do
newfile=$newfile.JPG
done
mv "$file" "$newfile"
done
done
(This also addresses the dupefile vs newfile error.)

Trying to call a script while passing two arguments [duplicate]

This question already has answers here:
Attempting to pass two arguments to a called script for a pattern search
(2 answers)
Closed 9 years ago.
I have a script that greps with $1 and $2, first argument being a pattern and second being a file.
I need to create another script that calls this first one, passes the two arguments to it, and if the second is a directory, loops it on all the files in the directory.
Does anyone know how I'd go about this? I keep coming close but failing miserably.
EDIT
Thought that the other post I had made didn't go through, Somehow got it lost. I apologize to everyone, so sorry.
Please forgive me. :(
if [[ -d $2 ]]; then
find "$2" -type f -exec ./script "$1" {} \;
else
./script "$1" "$2"
fi
If $2 is a directory then the find command finds all of the files in it and calls ./script once for each file. The curly braces {} are a placeholder for these file names.
Something like:
[[ -d "$2" ]] && grep -e "$1" -r "$2" || grep -e "$1" "$2"
It tests whether arg 2 is a directory (bash syntax) and if so it invokes grep in recursive mode, otherwise in non-recursive.

Check if file exist Linux bash [duplicate]

This question already has answers here:
How do I tell if a file does not exist in Bash?
(20 answers)
Difference between ./ and ~/
(6 answers)
Closed 2 years ago.
So I'm trying to check if a file exists or not and then the script is supposed to do something if it does. The problem I'm having is actually getting it to recognize that something is actually there.
if [ -e /temp/file.txt ]; then
echo "file found!"
sudo cp -r temp/* extra
else
echo "file not found! Creating new one..."
./create.sh
fi
below is an example of the files in the directory I'm testing. they are clearly there, but for some reason I can't get the script to see that. what am I doing wrong?
nima#mkt:/docs/text$ ls -a temp
. .. more file.txt file2.txt
You are using absolute paths in your test while you should be using relative paths:
if [ -e ./temp/file.txt ]; then
/temp/file.txt vs /docs/text/temp/file.txt?
You script looks in /temp while you are looking in /docs/text/temp

Resources