How to remove the file '--help' [duplicate] - bash

This question already has answers here:
How to remove files starting with double hyphen?
(7 answers)
Closed 7 years ago.
Ok, I somewhat stupidly created the file '--help'.
I don't know how exactly I created the file, but it is probably something like an accidental redirection of output. :
cat somefile.txt > --help
But what I mainly noted it's harder to delete such file, than meets the eye.
rm --help
or
mv --help removeme.txt
rm removeme.txt
See --help not as a file but an option.
The only method I could figure out was something like this
cd somedir
rm $OLDPWD/--help
Or even more lame delete it with fileroller or an other filemanager.
But there must be some way to do it right?

Use this:
rm -- --help
rm ./--help

That should do the trick:
rm -- --help
The -- basically says that everyting afterwards shall not be treated as an option.

Related

How to add suffix to multiple files in subdirectories based on parent directory?

I have 100+ directories as followed:
bins_copy]$ ls
bin.1/
bin.112/
bin.126/
bin.24/
bin.38/
etc. etc.
Each of these directories contains two files names genes.faa and genes.gff, e.g. bin.1/genes.faa
I now want to add a suffix based on the parent directory so each gene file has a unique identifier, e.g. bin.1/bin1_genes.faa and bin1_genes.gff.
I've been going down the google rabbit hole all morning and nothing has sufficiently worked so far.
I tried something like this:
for each in ./bin.*/genes.faa ; mv genes.faa ${bin%-*}_genes.faa $each ; done
but that (and several versions of it) gives me the following error:
-bash: syntax error near unexpected token `mv'
Since this is a really generic one I haven't figured it out yet and truly would appreciate your help with.
Cheers/
Try this Shellcheck-clean code:
#! /bin/bash -p
for genespath in bin.*/genes.*; do
dir=${genespath%/*}
dirnum=${dir##*.}
genesfile=${genespath##*/}
new_genespath="$dir/bin${dirnum}_${genesfile}"
echo mv -iv -- "$genespath" "$new_genespath"
done
It currently just prints the required mv command. Remove the echo when you've confirmed that it will do what you want.
There may be a more elegant way of doing this but create this script in the same directory as the bin directories, chmod 700 and run. you might want to back up with tar first (tar -cf bin.tar ./bin*)
#!/bin/bash
files="bin.*"
for f in $files; do
mv ./${f}/genes.faa ./${f}/${f}_genes.faa
mv ./${f}/genes.gff ./${f}/{$f}_genes.gff
done

How to make folders for individual files within a directory via bash script?

So I've got a movie collection that's dumped into a single folder (I know, bad practice in retrospect.) I want to organize things a bit so I can use Radarr to grab all the appropriate metadata, but I need all the individual files in their own folders. I created the script below to try and automate the process a bit, but I get the following error.
Script
#! /bin/bash
for f in /the/path/to/files/* ;
do
[[ -d $f ]] && continue
mkdir "${f%.*}"
mv "$f" "${f%.*}"
done
EDIT
So I've now run the script through Shellcheck.net per the suggestion of Benjamin W. It doesn't throw any errors according to the site, though I still get the same errors when I try running the command.
EDIT 2*
No errors now, but the script does nothing when executed.
Assignments are evaluated only once, and not whenever the variable being assigned to is used, which I think is what your script assumes.
You could use a loop like this:
for f in /path/to/all/the/movie/files/*; do
mkdir "${f%.*}"
mv "$f" "${f%.*}"
done
This uses parameter expansion instead of cut to get rid of the file extension.

Bash script sudo and variables [duplicate]

This question already has an answer here:
Bash foreach loop works differently when executed from .sh file [duplicate]
(1 answer)
Closed 4 years ago.
Totally new to Bash here, actually I've avoided it like a plague for 10 years.
Today, there is no way around it.
After a few hours of beating my head against the keyboard, I discovered that sudo and any bash variable in a command gets stripped out.
So I have something like
somescript.sh
for i in {1..5}
do
filename=somefilenumber"$i".txt
echo $filename
done
on the command line now if I run it
user#deb:~$ ./somescript.sh
I get the expected
somefilenumber1.txt
somefilenumber2.txt
somefilenumber3.txt
somefilenumber4.txt
somefilenumber5.txt
but if I run with sudo, like
user#deb:~$ sudo ./somescript.sh
I'll get this
somefilenumber{1..5}.txt
This is a huge problem because I'm trying to cp files and rm files in a loop with the variable.
So here is the code with cp and rm
for i in {1..10}
do
filename=somefilenumber"$i".txt
echo $filename
cp "$filename" "someotherfilename.txt"
rm "$filename"
done
I end up getting
cp: cannot stat 'somefilenumber{1..5}.txt': No such file or directory
rm: cannot remove 'somefilenumber{1..5}.txt': No such file or directory
I need to run sudo also because of other programs that require it.
Is there any way around this?
Even if nothing else require sudo, and I don't use it, the rm command will prompt me for every file if I'm sure that I want to remove it or not. The whole point is to not be sitting here tied to the computer while it runs through hundreds of files.
You could try to replace {1..10} with seq 1 10:
for i in `seq 1 10`
do
filename=somefilenumber"$i".txt
echo $filename
cp "$filename" "someotherfilename.txt"
rm "$filename"
done
Your problem sounds like the environment has something wrong for root, do you start the script with:
#!/bin/bash
?

do command 2 when command 1 fails in bash [duplicate]

This question already has answers here:
How can I "try to do something and then detect if it fails" in bash?
(3 answers)
Closed 8 years ago.
I run this as part of a 'unlock' bash script, but it fails on the first command -
# Variables
CHUNK="/media/backup/obnam-home"
BIGNUM="17580577608458113855"
LOGTO="/home/boudiccas/logs/unlock.txt"
####################################################
sudo rm $CHUNK/chunklist/lock; sudo rm $CHUNK/$BIGNUM/lock; sudo rm $CHUNK/chunksums/lock; sudo rm $CHUNK/chunks/lock>>'$(date -R)' $LOGTO
How can I get it to continue onto the second, and further commands, even if 'x' command fails?
I think this is what you want:
# Variables
CHUNK="/media/backup/obnam-home"
BIGNUM="17580577608458113855"
LOGTO="/home/boudiccas/logs/unlock-$(date -R).txt"
####################################################
{
sudo rm $CHUNK/chunklist/lock
sudo rm $CHUNK/$BIGNUM/lock
sudo rm $CHUNK/chunksums/lock
sudo rm $CHUNK/chunks/lock
} 2>> $LOGTO
Each of the four rm commands will run, regardless of which ones succeed and which fail. Any error messages from all 4 will be redirected (2>>, not >>) to the named file. I'm assuming you want the current timestamp in the file name, so I moved the call to date to the definition of LOGTO.

Bash shell: how to add a name

I am trying to rename some zip files in bash with an _orig but I seem to be missing something. Any suggestions??
My goal:
move files to an orig directory
rename original files with a "_orig" in the name
The code Ive tried to write:
mv -v $PICKUP/*.zip $ORIGINALS
for origfile in $(ls $ORIGINALS/*.zip);do
echo "Adding _orig to zip file"
echo
added=$(basename $origfile '_orig').zip
mv -v $ORIGINALS/$origfile.zip $ORIGINALS/$added.zip
done
Sorry still kinda new at this.
Using (p)rename :
cd <ZIP DIR>
mkdir -p orig
rename 's#(.*?)\.zip#orig/$1_orig.zip#' *.zip
rename is http://search.cpan.org/~pederst/rename/ (default on many distros)
Thanks to never use
for i in $(ls $ORIGINALS/*.zip);do
but use globs instead :
for i in $ORIGINALS/*.zip;do
See http://porkmail.org/era/unix/award.html#ls.
I know you've got a solution already, but just for posterity, this simplified version of your own shell script should also work for the case you seem to be describing:
mkdir -p "$ORIGINALS"
for file in "$PICKUP"/*.zip; do
mv -v "$file" "$ORIGINALS/${file%.zip}_orig.zip"
done
This makes use of "Parameter Expansion" in bash (you can look that up in bash's man page). The initial mkdir -p simply insures that the target directory exists. The quotes around $PICKUP and $ORIGINALS are intended to make it safe to include special characters like spaces and newlines in the directory names.
While prename is a powerful solution to many problems, it's certainly not the only hammer in the toolbox.

Resources