How does terminal read a file with a space in it? - terminal

When I try to open a file that is say 'blue face' on my desktop my terminal won't recognize it. So if I don't want to drag and drop the file how do I use cd to call file 'blue face' the right way. I tried using an underscore like this 'cd blue_face' but still nothing. What's the right way to call a file with a space in it in terminal?

You can either reference it with quotes:
cd "blue face"
Or by escaping the space with a backslash:
cd blue\ face

You can use: cd 'blue face': the name of the file between single quotes.

Related

source a file in .zshrc does not find the file

I'm using zsh on Catalina and I have some problems with sourcing (dunno if that's the correct term) a file in my .zshrc file that has a white space in the path.
Basically what I'm trying to achieve is to have my personal zsh settings in iCloud so that it syncs between all my computers. There is a white space in the path to my iCloud directory that I can't change.
~/Library/Mobile Documents/com~apple~CloudDocs/
Escaping the wihte space does not work when I try it.
file='~/Library/Mobile\ Documents/com~apple~CloudDocs/file.sh'
source $file
Results in:
.zshrc:source:29: no such file or directory: ~/Library/Mobile\ Documents/com~apple~CloudDocs/file.sh
It works fine when I manually do:
source ~/Library/Mobile\ Documents/com~apple~CloudDocs/file.sh
Any ideas on how to solve this or I'm I back at manually load my files?
The quotes are ruining your day, and the excaped space doesn't make much sense either, because you are quoting the whole stuff anyway:
file='~/Library/Mobile\ Documents/com~apple~CloudDocs/file.sh'
This means that you need a directory with the name ~.
You could write it as
file=~/Library/Mobile\ Documents/com~apple~CloudDocs/file.sh
which would substitute the tilde by your home directory. I personally would use double quotes and write it as:
file="$HOME/Library/Mobile Documents/com~apple~CloudDocs/file.sh"

MacOS Terminal go to a folder with spaces and parenthesis

I'm trying to configure Cyberduck to read the Bookmark files from my Dropbox folder.
This is usually accomplished by this command:
defaults write ch.sudo.cyberduck application.support.path ~/Dropbox/Cyberduck
Super easy!!
But... Dropbox has since changed and if you have a Pro Account the Dropbox folder is renamed "Dropbox (Personal)".
I've tried to do this:
defaults write ch.sudo.cyberduck application.support.path ~/Dropbox\ \(Personal\)/
And I get this error:
not parse: [...]/Dropbox (Personal)/Apps/Cyberduck/. Try single-quoting it.
I tried single quoting like this but same error:
defaults write ch.sudo.cyberduck application.support.path '~/Dropbox\ \(Personal\)/'
How can I solve this?
You can single quote escape your parentheses by wrapping them in a double quote:
Instead of: ln -s '/Users/username/Dropbox (Company Name)/' DropboxCompanyName
Do: ln -s "'/Users/username/Dropbox (Company Name)/'" DropboxCompanyName
Notice the double quotes added to /Users/username/Dropbox (Company Name)/
So I cannot find a way to do a "defaults write" to a path with parentheses, but I did this workaround and it seemed to work for me (in my case I needed to link to my enterprise Dropbox account):
cd to your home folder and create a symlink of the directory that has the parentheses:
ln -s '/Users/username/Dropbox (Company Name)/' DropboxCompanyName
At that point, I was able to do do a defaults write that wrote ~/DropboxCompanyName as part of the path and it worked just fine.
All that said, your personal folder already has a hidden symlink: "Dropbox" that's in the same directory and pointing to "Dropbox (Personal)", so you should be able to do you original command as such:
defaults write ch.sudo.cyberduck application.support.path ~/Dropbox/Cyberduck
… because the "Dropbox" part of the path should still lead to your personal folder. This both what I've observed locally (when viewing hidden files) and what Dropbox says on their site: https://www.dropbox.com/help/9031
I am assuming you are using Unix in Terminal.
I have set up a folder of the same name in my Public Folder to test as shown below.
Your current referencing to the folder would seem correct. Dragging the folder into the Terminal window current command line will automatically give you the correct referencing to that folder.
As an alternative I suggest putting the name of the folder in double quotation marks. Even though there are brackets in the name, there is no need to escape these characters in Unix in Terminal in this instance when using double quotation marks. This makes it easier to humanly type the correct reference.
To reference a folder in the image below, for example, the following referencing works for the cd (change directory command):
~/Public/"Dropbox (Personal)"
Hence I suggest try:
ch.sudo.cyberduck application.support.path ~/Public/"Dropbox (Personal)"
As for the rest of this command, I am not sure that ch is a valid Unix command. I do not yet have enough Unix experience to guide you from here.
you have to put "" in the whole address
more like this
ch.sudo.cyberduck application.support.path "~/Public/Dropbox (Personal)"

cd to specific directory by using part of input string and wildcard

I have multiple files in different directories. They look like this
/home/usr/data/folder_K123_A01/file/Sample_K123_1_ID33/K123_1_ID33.txt
/home/usr/data/folder_J337_B02/file/Sample_J337_4_ID46/J337_4_ID46.txt
/home/usr/data/folder_L711_C03/file/Sample_L711_13_ID07/L711_13_ID07.txt
A01, B02, C03 are generated by machine; not really specific. It can use * to autofilled those values.
If I want to move to L711_13_ID07's directory, I use
cd /home/usr/data/folder_L711*/ -> tab, enter
cd /file
cd /Sample_L711_13_ID07
Right now, need to move among those directories quickly. So, trying to compose script movequick.sh to take input filenames and cd to each directory quickly.
Current code is
Filename=$1
FD=`echo $Filename|cut -f1 -d'_'`;
cd /home/usr/data/folder_$FD_*/file/Sample_$Filename/
I do
./movequick L711_13_ID07
But, system gives me
line 3: cd: /home/usr/data/folder_L711_*/file/Sample_L711_13_ID07/: No such file or directory
I think I am missing one tab. I am also trying to put this script into my .bashrc to assure I can move around quick.
Need some advise. Thanks.
You can use:
filename="$1"
fd="${filename%%_*}"
cd /home/usr/data/folder_"${fd}"_*/file/Sample_"${filename}"/
Important to use ${fd} as you are using _ after this variable name and _. In the variable name $fd_ underscore is considered part of variable name.
When the folders are generated once and fixed after that, you can set up a bunch of aliases like
cdk123="cd /home/usr/data/folder_K123_A01/file/Sample_K123_1_ID33/K123_1_ID33.txt"
cdj337="cd /home/usr/data/folder_J337_B02/file/Sample_J337_4_ID46/J337_4_ID46.txt"
cdl711="cd /home/usr/data/folder_L711_C03/file/Sample_L711_13_ID07/L711_13_ID07.txt"
Put these in .bashrc and after login (or sourcing the file) you can use things like cdk123.
When the folders are fixed during your login session, you can generate these aliases from the .bashrc.
When it is variable during your session, you can use wildcards in your cd command:
cd /home/usr/data/*/*/*L711_13_ID07
Do you want to use TAB, you do not need to use enter all the time:
If you want to move to L711_13_ID07's directory, use
cd /home/usr/data/folder_L711 -> tab f -> tab S -> tab ENTER
Play with the tab option before trying to use a script. When bash doesn't know which file/folder you want (two dirs both starting with f), hit tab again: after 2 tabs you see a list of possibilities. Add some letters and hit tab again.
And when you have cd commands in your history, try ^r cd and hit ^r until you see something.

Copy file path to terminal without escape sequences in mac

I have a perl script that takes input file path from the terminal. So, while entering the file path, I can drag and drop the file to the terminal (Mac). If I have file paths like
/Users/San/abc(ev50)_xyz.tif,
then dragging and dropping the file will result in
/Users/San/abc\(ev50\)_xyz.tif
being pasted on the terminal. Is there a way to copy paste (drag and drop) the file path as is? I saw in some of the forums, they had mentioned about creating a service and so on. But, I want to know if there is an easy way to paste it as is.
Drag/drop enters the filename, with characters that might present problems to shell scripts escaped with backslashes (such as bash). Reading the filename directly, you of course do not have this problem. The solution is to remove the extra backslashes, e.g., as done in #Ether's answer to How can I unescape backslashes in a Perl string?.

Running python batch file that has a path with SPACE character

The batch file is something like this, I put the python in some directory that has SPACE character in its path.
C:\"Documents and Settings"\Administrator\Desktop\bracket\python\python
C:\\"Documents and Settings"\\Administrator\\Desktop\\bracket\\[10,20]\\brackettest.py
When I run this one, I get this error.
C:\Documents and Settings\Administrator\Desktop\bracket\python\python: can't ope
n file 'C:\Documents and Settings\\Administrator\\Desktop\\bracket\\[10,20]\\bra
ckettest.py': [Errno 2] No such file or directory
C:\Documents and Settings\Administrator\Desktop\bracket>
What might be wrong?
Wrapping the path doesn't solve this problem.
"C:\\Documents and Settings\\Administrator\\Desktop\\bracket\\[10,20]\\brackettest.py"
Are the brackets ('[]') cause of the problem? On Mac, python works well with bracket character.
There's no backslash escaping in the Windows console, it should read
"C:\Documents and Settings\Administrator\Desktop\bracket\python\python" "C:\Documents and Settings\Administrator\Desktop\bracket\[10,20]\brackettest.py"
Looks like you almost had it; just replace the double-slashes with single slashes.
If you're ever unsure and need a reminder of the right format. Try using the tab key to automatically complete your folder or filename. For example, at your command prompt just type:
C:\docu
then hit the tab key and it will automatically complete your folder name. From there you just type in the rest. Whenever you want to automatically complete the rest of the folder or filename, just hit the tab key. This will always produce a properly specified path that is runnable.
path = r"C:\Users\mememe\Google Drive\Programs\Python\file.csv"
Closing the path in r"string" also solved this problem very well. This tells python that it is exactly that string and avoids backslash and space errors. I use it for my files because it allows me to copy and paste directly from the navigation bar at the top of the window.
Can you
cd "\Documents and Settings"
cd "\Documents and Settings\Administrator\Desktop\bracket\python\python"
cd "\Documents and Settings\Administrator\Desktop\bracket\"
cd "\Documents and Settings\Administrator\Desktop\bracket\[10,20]\"
If all that works, do
cd "\Documents and Settings\Administrator\Desktop\bracket\python\python"
python "\Documents and Settings\Administrator\Desktop\bracket\[10,20]\brackettest.py"
Why do you have such a weird, nondescriptive dirname ([10,20])? Didn't even know that NTFS allowed that.

Resources