why does command line rm not accept quotation marks for directories with spaces? - linux

Running rm projects/artwork/My Project (543893)/Images/*.png at the command line in Debian does not work because of the spaces and the parenthesis. I need to escape them. But I thought quotation marks "" were an alternative to escaping. It works with other commands, such as cd, but not with rm. Why is that?

Because globbing does not work with quoting:
$ ll /tmp/"d i r"/*.png
-rw-r--r--. 1 postgres postgres 0 May 26 14:02 /tmp/d i r/a.png
-rw-r--r--. 1 postgres postgres 0 May 26 14:02 /tmp/d i r/p.png
$ ll "/tmp/d i r/*.png"
ls: cannot access /tmp/d i r/*.png: No such file or directory
$ rm "/tmp/d i r/*.png"
rm: cannot remove ‘/tmp/d i r/*.png’: No such file or directory
$ rm /tmp/"d i r"/*.png
$ ll /tmp/"d i r"/*.png
ls: cannot access /tmp/d i r/*.png: No such file or directory

You should ensure that:
the spaces and parentheses are inside the quotation marks, so that they are treated as literals, because otherwise these characters will have special meanings for the shell (spaces to separate command-line arguments, and parentheses for a subshell command)
but for the * you want the exact opposite: it must be outside the quotation marks so that it is indeed given its special meaning as a wildcard, not a literal *.
Remember that you do not need to apply quotation marks to the whole string, only the part that needs treating as a literal and otherwise would be given a special meaning by the shell.
So for example you could use:
rm projects/artwork/"My Project (543893)"/Images/*.png
Similarly, if you use escapes, escape the spaces and parentheses, but not the wildcard:
rm projects/artwork/My\ Project\ \(543893\)/Images/*.png
(In other words, you would not use \*.)

Related

how to specify path with space inside file in linux

cat ~/.last_dir
/mnt/c/Users/Administrator/OneDrive/Desktop/main project/backup/main project 2
cd cat ~/.last_dir
-bash: cd: too many arguments
I tried using backslash inside the file
/mnt/c/Users/Administrator/OneDrive/Desktop/main\ project/backup/main\ project\ 2
Still same error
You need to quote the results of expanding cat ...:
cd "$(cat ~/.last_dir)"
cd "$(<~/.last_dir)"
First, put quotes around the $(...) to make the space part of the filename.
Second, $(<...) is a bash construct that reads the file directly without executing cat, but is not entirely portable.
For a more generic, less bash-specific version, use Maxim's solution.
just put quotation marks around your path:
"/mnt/c/Users/Administrator/OneDrive/Desktop/main project/backup/main project 2"
this should work for most cases

Linux command and single quote

I thought the single quotes simply reserve the literal value of each character within the quotes and do not interfere with the command result except for those character-escaping situations.
For example, the two commands below returned the same result.
$ ls /home/testuser/tmp1
$ ls '/home/testuser/tmp1'
This command below ran successfully.
$ cp /home/testuser/tmp1/* /home/testuser/tmp2
However, the command below ran into error.
$ cp '/home/testuser/tmp1/*' /home/testuser/tmp2
cp: cannot stat '/home/testuser/tmp1/*': No such file or directory
What did I do wrong here?
* is what you call a character escaping situation. Since you preserved the literal value of *, it lost its meaning of "all files", and cp instead tries to copy a file named literally *.
When you run:
cp /home/testuser/tmp1/* /home/testuser/tmp2
The shell will transparently rewrite it into:
cp /home/testuser/tmp1/bar /home/testuser/tmp1/foo /home/testuser/tmp2
This process is known as "pathname expansion" or "globbing". When you quote the *, this rewrite does not happen.

Wildcards as shell parameters

I know how regex and wildcards work in general, but I don't really understand why you can use them as parameters.
ls /[!\(][!\(][!\(]/
command results in the following output
...
com.apple.launchd.AIPZ6SAfpO
com.apple.launchd.HarlOx3LWS
com.apple.launchd.VmTi5KDz1h
powerlog
/usr/:
X11 include libexec sbin standalone
bin lib local share
/var/:
agentx empty log netboot rwho
at folders ma networkd spool
audit install mail root tmp
backups jabberd msgs rpc vm
db lib mysql run yp
from my understanding this should match every three character folder name not containing slash /[!\(][!\(][!\(]/
But why can I use it as parameter?
You can't use regular expressions as parameters (or rather, the shell will not treat a string as a regular expression when placed in a parameter). The unquoted glob /[!\(][!\(][!\(]/ matches, in order:
A slash.
Three characters which are not starting brackets.
A slash.
In other words, three-letter root directories not containing ( anywhere.
The shell expands globs to zero (in case of Bash's nullglob, for example) or more arguments which may be passed to execve, as in this command:
$ strace -fe execve echo *
execve("/usr/bin/echo", ["echo", "directory1", "directory2"], 0x7ffcff705ce8 /* 44 vars */) = 0
Not, you don't know.... shell patterns are described in glob(3) while regular expressions (a more elaborate concept) are described in regex(3) Two different libraries used for similar purposes. sh(1) doesn't use regular expressions when substituting parameters at all. It only uses the glob(3) library.
Because that's how the shell works. Any arguments containing (unquoted) glob characters/expressions, are expanded to filenames. That's what happens in, say rm *.txt (since * is a glob character), and that's what happens in ls /[!\(][!\(][!\(]/ (since [abc] is a glob expression).
They're not regular expressions, though. See e.g. https://mywiki.wooledge.org/glob for the syntax.

bash handling of quotation marks in filename

I am trying to remove and replace quotation marks that are present in a file name. For example, I would like to change:
$ ls
abc"def"ghi"jkl"mno
to this
$ ls
abc:def:ghi:jkl:mno
In trying to solve this, I came across How to rename a bunch of files to eliminate quote marks, which is exactly what I want to do. However, it didn't work for my case. To figure out why, I tried creating a test file like this:
$ touch abba\"abba\"cde\"cde\"efef
With this file, the solutions I came across (such as mentioned above) worked. But why didn't it work for the first file?
One thing I discovered was that bash command completion sees them differently. If I type in
$ ls abb<tab>
bash will complete the filename like so:
$ abba\"abba\"cde\"cde\"efef
just as I created it. But for the original file, bash completion went like this:
$ ls abc<tab>
results in
$ abc"def"ghi"jkl"mno
So in the test case file, there is an escape of the quotation marks, and in the other case (the file I really want to rename), there is no escaping of the the quotation marks. I don't know how the original files were named.
Can anyone explain why bash sees these names differently, and how I would go about renaming my file?
Here is two ways to rename a file with "(quotation) mark,
option 1: With escape character \
mv abc\"cdf\"efg\"hij newFileName
option 2: By using '(single quote)
mv 'abc"cdf"efg"hij' newFileName
Note: using special charaters like :(colon) in file name might not be a good idea,
and regarding the auto completion, it usually fill the name with escape character, example
ls abc<tab> will complete the name to ls abc\"cdf\"efg\"hij
unless you start the name with a quote, example
ls 'abc<tab> will complete the name to ls 'abc"cdf"efg"hij'

linux batch rename directories and strip # character from name

i have a directory with a lot of subdirectories with a # infront of them:
#adhasdk
#ad18237
I want to rename them all and remove the # caracter
I tried to do:
rename -n `s/#//g` *
but didn't seem to work.
-bash: s/#//g: No such file or directory
Any ideas on this.
Thanks
Just use
$ rename 's/^#//' *
use -n just to check that what you think it would happen really happens.
In you example you have the clue about the wrong quotes used (backticks) in the error message
-bash: s/#//g: No such file or directory
bash is trying to execute a command named s/#//g.
No that using g (global) and not anchoring the regular expression you will replace any #, not just the one in the first position.
I don't know whether it's just a typo when you typed it here, but that "rename" command should work if:
you leave off the "-n" and
you quote the substitution with regular single-quotes and not back-quotes
The "-n" tells it to not really do anything. The back-quotes are just wrong (they mean something but not what you want here).
The problem is that you use backticks (`). You should use normal quotes:
rename -n 's/#//g' *
for DIR in \#*/
do
echo mv "$DIR" "${DIR/#\#/}"
done
I had to rename all folders inside a given folder. Each folder name had some text inside round braces. The following command removed the round braces from all folder names:
rename 's/(.+)//' *
Some distros doesn't support regexp in rename. You have to install prename. Even more, sometimes you can't install prename and you have to install gprename to have binary prename.
If you have 'prename' then just change backtick character " ` " to single quote and everything should work.
So the solution should be:
prename -n 's/#//g' *
or
prename -n 'y/#//' *

Resources