How to create a folder with a folder name containing spaces in linux?
you can also do mkdir Stack\ OverFlow
the \
does the same thing as " "
but it is easier. so you can do stuff like mkdir Stack\ OverFlow\ is\ Great
. you can manipulate that folder using the \
as well. so things like cd Stack\ OverFlow
and rm -rf Stack\ OverFlow
.
Get the name of the folder from a path with whitespace
When dealing with whitespaces, all variables should be double-quoted when passed as command line arguments, so bash would know to treat them as a single parameter:
mypath="/Users/ckull/Desktop/Winchester stuff/a b c/some other folder/"
dir="$(basename "$mypath")" # quote also around $mypath!
echo "lookig in $dir"
# examples
ls "$dir" # quote only around $dir!
cp "$dir/a.txt" "$dir/b.txt"
This is how variable expansion occurs in bash:
var="aaa bbb"
# args: 0 1 2 3
foo $var ccc # ==> "foo" "aaa" "bbb" "ccc"
foo "$var" ccc # ==> "foo" "aaa bbb" "ccc"
foo "$var ccc" # ==> "foo" "aaa bbb ccc"
Shell Script to Enter a Directory (Folder Name Contains Spaces)
This is the (principal) function of double quotes, and it's true in csh and *sh shells.
cd "$TARGET"
should do it.
Shell variables are expanded within "..."
(unlike within '...'
), but the quoted text is regarded as a single argument when the shell parses the command line to construct the strings which are passed to the program.
For example:
% ls -F
October @012/
% TARGET="October @012"
% cd $TARGET
bash: cd: October: No such file or directory
% cd "$TARGET"
% pwd
/tmp/t/October @012
%
Simple!
What you're doing wrong in your initial example is escaping the space inside the quotes. The space doesn't have to be escaped twice, and because this redundant \
is appearing inside the quotes, it just inserts a backslash into the TARGET variable. For example:
% TARGET="October\ @012" # wrong!
% ls
October @012/
% cd $TARGET
bash: cd: October\: No such file or directory
% cd "$TARGET"
bash: cd: October\ @012: No such file or directory
%
This setting of TARGET
would only work if the directory were named October\ @012
, with a backslash in it (not recommended!):
% mkdir October\\\ @012
% ls -F
October\ @012/
% cd "$TARGET"
% pwd
/tmp/t/October\ @012
%
(EDITED to add example)
Git add a folder with spaces in the name
The solution is to wrap the folder name inside ' and ' (single quotes).
In your example, try the following:
git add 'Folder A'
I hope this helps :)
How to create a folder with spaces written in Java under Linux?
runtime.exec(new String[] { "mkdir", "My Folder" });
How can I go to a directory whose file name has spaces between them in the Linux terminal?
You need to quote the directory name:
cd 'Magical Island'
or escape the space character:
cd Magic\ Island
Shell script issue with directory and filenames containing spaces
Use find -print0 | xargs -0
to reliably handle file names with special characters in them, including spaces and newlines.
find /bishare/IRP_PROJECT/SFTP/ -type f -print0 |
xargs -0 ls -al > "$Temp_Path/File_Posted_$CURRENT_DATE.txt"
Alternatively, you can use find -exec
which runs the command of your choice on every file found.
find /bishare/IRP_PROJECT/SFTP/ -type f -exec ls -al {} + \
> "$Temp_Path/File_Posted_$CURRENT_DATE.txt"
In the specific case of ls -l
you could take this one step further and use the -ls
action.
find /bishare/IRP_PROJECT/SFTP/ -type f -ls > "$Temp_Path/File_Posted_$CURRENT_DATE.txt"
You should also get in the habit of quoting all variable expansions like you mentioned in your post.
How to cd into a directory with space in the name?
$ cd "$DOCS"
You need to quote "$DOCS"
to prevent spaces from being parsed as word separators. More often than not, variable references should be quoted.
Note that $HOME
would have the same problem. The issue is coming from when the shell evaluates variable references; it's nothing to do with what variables you use or how you assign to them. It's the expansion that needs to be quoted.
$ echo $HOME
/home/my dir
This is deceptive. echo
is actually echoing the two strings /home/my
and dir
. If you use cd
or ls
you'll see how it's actually working.
$ ls $HOME
ls: cannot access /home/my: No such file or directory
ls: cannot access dir: No such file or directory
$ cd $HOME
bash: cd: /home/my: No such file or directory
$ cd "$HOME"
<success!>
Can I ask why it works when I manually type it in but not in a variable?
Great question! Let's examine the commands you typed:
$ DOCS="\"/cygdrive/c/Users/my dir/Documents\""
$ echo $DOCS
"/cygdrive/c/Users/my dir/Documents"
$ cd $DOCS
-bash: cd: "/cygdrive/c/Users/my: No such file or directory
The reason this doesn't work is because Bash doesn't parse quotes inside variable expansions. It does perform word splitting, so whitespace in unquoted variable expansions is taken as word separators. It doesn't parse quotes in any way, meaning you can't put double quotes inside a variable to override word splitting.
$ cd $DOCS
Because of this, cd
is passed two parameters. As far as cd
knows it looks like you wrote:
$ cd '"/cygdrive/c/Users/my' 'dir/Documents"'
Two parameters, with double quotes intact.
deal with filename with space in shell
You shouldn't use ls
in for
loop.
$ ls directory
file.txt 'file with more spaces.txt' 'file with spaces.txt'
Using ls
:
$ for file in `ls ./directory`; do echo "$file"; done
file.txt
file
with
more
spaces.txt
file
with
spaces.txt
Using file globbing:
$ for file in ./directory/*; do echo "$file"; done
./directory/file.txt
./directory/file with more spaces.txt
./directory/file with spaces.txt
So:
for file in "$rule"/*.gz; do
echo "$file"
#tar -xf *.gz
#mv a b.xml to ab.xml
done
Dealing with files/folders with spaces in bash script
find
has the -print0
flag to deal with this problem:
#!/bin/bash
find . -print0 | while read -d $'\0' file
do
echo ${file}
done
Example:
$ ls
script.sh space name
$ ./script.sh
.
./script.sh
./space name
Related Topics
Command Execution with Nohup in Background
Linux Intel 64Bit Assembly Division
Multi-Architectural Binary Rpm and The Noarch
Starting with Kde Frameworks 5 and Qt Creator
Passing a Command with Arguments as a String to Docker Run
Brother Ql-720Nw Specifying Media Size Seems Ignored
How to Issue "Module Load" in a Shell or Perl Script (I.E., Non-Interactively)
Get Apache Total CPU Usage in (Linux)
Xdotool Type Takes Ages and Causes Entire Desktop to Freeze
Function to Search of Multiple Patterns Using Grep
Prevalence of 64Bit Vs 32Bit Platforms
How to Install Vsftpd on Centos 6
How to Script Multiple Ssh and Scp Commands to Various Systems
How to Insert a Tab Character in Iterm
Gcc: Linked Libraries in /Usr/Local/Lib Are Not Found, But /Etc/Ld/So.Conf.D/Libc.Conf Lists It
Arduino Upload Error "Stk500_Recv(): Programmer Is Not Responding" in Fedora