Opening a Python file in desktop folder - python-3.x

Working on a Mac I have individual .py files on my desktop that open and run fine when typing the following in the terminal:
cd desktop
python3 filename.py
Now, I created a folder named Python on the Desktop to better store these files but I can't seem to access it, no matter what I try. I have tried the following
cd desktop/Python (file or directory doesn't exist)
cd desktop/Python python3 filename.py (file or directory doesn't exist)
Any ideas?

cd desktop/Python # (file or directory doesn't exist)
There are two kinds of path specifications: paths can be absolute or relative. An absolute path begins with /. A path that does not begin with / is interpreted relative to the current directory. desktop/Python is a relative path. The directory that it indicates varies depending on what the current directory is.
~/desktop/Python is, by contrast, an absolute path. It points the same place regardless of what the current directory is. If one wants a cd command to work without knowing where one currently is in the directory structure, an absolute path should be used:
cd ~/desktop/Python
Or:
cd "$HOME/desktop/Python"
The shell and tilde expansion
The shell interprets ~/ to mean the home directory, $HOME. Note, though, that this is only done if ~/ is outside of quotes. If it is in quotes, either single or double quotes, it is a relative path referring to a direct literally named ~.

Related

What is the Root directoy in a Linux System? [duplicate]

When creating filepaths and URLs, I noticed that many times the path starts with ./ or ~/.
What is the difference between filepaths that start with ./ and ~/?
What do each of them mean?
For the sake of completeness ...
Just path is a file or directory named path in the current directory.
./path is a file or directory named path in the current directory, with the directory spelled out. The dot directory . represents the current directory, and path is the name of the file or directory within this directory.
~/path is a shorthand for $HOME/path where $HOME is a variable which refers to your home directory. Typically your home directory will be somewhere like /home/you or /Users/you where you is your account name. (The command echo "$HOME" will display your home directory.) The expanded value is an absolute path (unless you have messed up the value of $HOME thoroughly), as indicated by the initial slash.
/path is an absolute path which refers to a file or directory named path which is in the root directory /. Every file on Unix is ultimately somewhere in the directory tree which starts with the root directory.
A file name which begins with $ includes the value of a shell variable in its name (like for example $HOME above); you have to know the value of that variable to determine whether it ends up containing a relative or an absolute path. Similarly, ~ at the beginning of a file name gets replaced ("expanded") by the shell to a different string, as outlined above.
(Technically, it's possible for a file name to begin with a literal dollar sign or tilde, too; you would then have to quote or backslash-escape that character to avoid having the shell expand it to something else. This is rather inconvenient, so these file names tend to be rare in practice.)
In the following exposition, we refer to the result of any such replacements, and ignore the complication of possible quoting.
Every file name which begins with / is an absolute path (aka full path) which explains how to reach a particular node starting from the root directory. For example, /var/tmp/you/reminder.txt refers to a file or directory reminder.txt (probably a file, judging from the name; but Unix doesn't care what you call your files or directories) which is in the directory you which is in the directory tmp which is in the directory var which is in the root directory.
Every file name which doesn't begin with / is a relative path which indicates how to reach a particular file or directory starting from the current directory. The special directory .. is the parent directory (that is, the directory which contains this directory) and the special directory . is the current directory. So path/there refers to the file or directory there inside the directory path in the current directory; and (hover the mouse over the gray area to display the spoiler)
there/.././and/back/.. is a (wicked complicated) way to refer to the directory and in the current directory, where we traverse the there directory and then move back to the current directory; then stay in the current directory; then refer to the directory back inside the directory and, but then move back to the parent directory of that, ending up with ./and.
In addition to ~/ for the current user's home directory, some shells and applications allow the notation ~them/ to refer to the home directory of the user account them. Also, some web server configurations allow each user to have a public web site in their directory ~/public_html and the URL notation http://server/~them/ would serve up the site of the user account them for outside visitors.
The current directory is a convenience which the shell provides so you don't have to type long paths all the time. You can, if you want to.
/bin/ls /home/you/Documents/unix-101/directories.txt
is a longwinded but perfectly valid way to say (assuming you are in your home directory),
ls Documents/unix-101/directories.txt
You could also say
cd Documents/unix-101
ls directories.txt
and until you cd again, all your commands will run in this directory.
See What exactly is current working directory? for a longer exposition of this related concept.
A "directory" is sometimes called a "folder" by people who are not yet old enough to prefer the former.
Tangentially, don't confuse the directory name . with the Bourne shell command which comprises a single dot (also known by its Bash alias source). The command
. ./scriptname
runs the commands from the file ./scriptname in the context of the current shell instance, as opposed to in a separate subshell (which is what just ./scriptname does). In other words, this command line invokes the dot command on a file scriptname in the dot directory.
The Bourne shell (and derivatives like Bash, Zsh, etc) use single quotes to prevent variable expansion and wildcard expansion, and double quotes to permit variable expansion, but inhibit wildcard expansion in a string. The quoting rules on Windows are different, and generally use double quotes to keep whitespace-separated values as a single string (and % instead of $ for variable substitutions).
./ means "starting from the current directory". . refers to the current working directory, so something like ./foo.bar would be looking for a file called foo.bar in the current directory. (As a side note, .. means refers to the parent directory of the current directory. So ../foo.bar would be looking for that file one directory above.)
~/ means "starting from the home directory". This could have different meanings in different scenarios. For example, in a Unix environment ~/foo.bar would be looking for a file called foo.bar in your home directory, something like /home/totzam/foo.bar. In many web applications, ~/foo.bar would be looking for a file called foo.bar in the web application root, something like /var/http/mywebapp/foo.bar.
./ is the current directory
~/ is the home directory of the current user
./ means that path is relative to your current position.
~/ means that path is relative to your home directory.
I will explain a simple example of it. As developers mentioned:
./ is current directory.
~/ is the home directory of the current user.
How both of the file path expressions can help us? Suppose you want to execute a script (.sh) and you're in the same directory where file exists then you can simply do it ./filename.sh
I mostly use ~/ to access my home directory files like .bashrc when I want to add any config in it. It's easier since the file path expression (for home directory) feels much easier and makes accessibility to the file from anywhere, without worrying about the path or changing the path.
. represents current directory
.. represents the parent directory
~ represents the home directory for the current user. Home directory is also represented by HOME env variable. you can do echo $HOME on the shell to see it.
These are generally used to specify relative paths. The / in the end of each notation is a separator that you can use when using these notations together.
Ex:
$ cd ../.. # Go 2 directories backwards
$ cd ~ # Takes you to $HOME directory
$ cd . # Does nothing :) As it literally means go to the directory that you are already present in.
$ cd ~/dir1 $ go to `$HOME/dir1`
On Unix, in any directory if you do ls -a you would see that . and .. will be mentioned (even for empty directory). Like mentioned, these have special meaning and are generated by default in Unix systems and are generally helpful to specify relative paths (i.e, path to a different directory relative to your current directory)
cd command is harmless. So, just play around by combining notations with cd command. You will eventually get a grip of them.

trying to create a file structure with relative path

I'm trying to create a file structure in unix using the relative path, however the directories are being created under my current directory? What am i doing wrong.
mkdir ../folder1 -p folder2/folder3 folder2/folder4 folder5
.. goes back to my home and /folder should go into folder1, but all of this is being done my current directory?
All the filename arguments to mkdir are simply directories to create, they're not interpreted relative to each other. Putting ../folder1 first doesn't make it use that as the starting directory for all the rest.
You can use brace expansion to concatenate all the subdirectory paths to a single directory prefix.
mkdir -p ../folder1/{folder2/{folder3,folder4},folder5}
Also, remember that options usually go before filename arguments.

how to list the classlist file with the absolute and relative path using LINUX command line?

I have a question regarding LINUX command line that i don't really understand:
Run the following commands.
$ cd
$ mkdir hw1-test
$ cd hw1-test
$ls /class/home > classlist
$ cd
what does the fourth command do?
Question:
Show the ls command you ran with the absolute path.
Show the ls command you ran with the relative path.
4th command lists out all the file in the directory to filename classlist in the same directory.
What is an absolute path?
An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory
What is the relative path?
Relative path is defined as path related to the present working directory(pwd). Suppose I am located in /var/log and I want to change directory to /var/log/kernel. I can use relative path concept to change directory to kernel.
Examples :
changing directory to /var/log/kernel by using relative path concept.
pwd
/var/log
cd kernel
Note: If you observe there is no / before kernel which indicates it’s a relative directory to present working directory.
Changing directory to /var/log/kernel using absolute path concept.
cd /var/log/kernel
Example 2: Present location is /abc/xyz, I am want to remove /abc/xyz/read/hello.txt file.
Using relative path:
rm read/hello.txt
Using absolute path:
rm /abc/xyz/read/hello.txt
SO ANSWERS TO YOUR QUESTIONS ARE
Show the ls command you ran with the absolute path
so absoulte path means full path.
Just open your terminal and you should know path of the directory you want to visit.
As you said in the question your path is /class/home/ i.e class is in root folder and home is inside it and you want to list out files in it so type
ls /class/home/ > classlist
Show the ls command you ran with the relative path
so now for relative path you will have to ennter into the directory one before the actual directory of home i.e. class
when you open your terminal you are in home directory by default i.e /Username/home
so type
cd .. //it will take you back into class directory
if you want to check you can check it with `pwd`.
and it will show your present working directory.
do
ls home/ > classlist

No man page for the cd command

Ubuntu Linux 15.10 - I just noticed that there is no man page for cd
This seems a bit strange.
I tried:
man cd
at the cmd line and I get back
No manual entry for cd
I was trying to find documentation on
cd -
which is super-handy for flipping between the last dir and the current dir
and cd --
which seems to be an alias for
cd ~
Am I missing something very obvious here, or should the man page be present?
cd is not a command, it's built into your shell. This is necessary because your current working directory is controlled by the PWD environment variable named after the pwd or "print working directory" command.
The environment variables of a parent process cannot be changed by a child process. So if your shell ran /bin/cd which changed PWD it would only affect /bin/cd and anything it ran. It would not change the shell's PWD.
Some systems, like OS X and CentOS, map the cd man page to builtin which lists all the shell built ins and lets you know you should look at your shell's man page.
You can check what shell you have with echo $SHELL, it's probably bash.
cd is a builtin shell command.
$ type cd
cd is a shell builtin
You can open a help page for cd on Bash with
$ help cd
Which currently shows (Ubuntu 16.04):
$ help cd
cd: cd [-L|[-P [-e]] [-#]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed: resolve symbolic links in
DIR after processing instances of `..'
-P use the physical directory structure without following symbolic
links: resolve symbolic links in DIR before processing instances
of `..'
-e if the -P option is supplied, and the current working directory
cannot be determined successfully, exit with a non-zero status
-# on systems that support it, present a file with extended attributes
as a directory containing the file attributes
The default is to follow symbolic links, as if `-L' were specified.
`..' is processed by removing the immediately previous pathname component
back to a slash or the beginning of DIR.
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
Unfortunately, it does not answer your questions. There is documentation that does, however.
You can get to it with
$ man builtins
It opens many pages of help with less, my default viewer. I can find the help for cd by pressing the / key, then typing cd, then Enter, and pressing n twice gets me to the third instance of the substring, and the help, which reads:
cd [-L|[-P [-e]] [-#]] [dir]
Change the current directory to dir. if dir is not supplied,
the value of the HOME shell variable is the default. Any addi‐
tional arguments following dir are ignored. The variable CDPATH
defines the search path for the directory containing dir: each
directory name in CDPATH is searched for dir. Alternative
directory names in CDPATH are separated by a colon (:). A null
directory name in CDPATH is the same as the current directory,
i.e., ``.''. If dir begins with a slash (/), then CDPATH is not
used. The -P option causes cd to use the physical directory
structure by resolving symbolic links while traversing dir and
before processing instances of .. in dir (see also the -P option
to the set builtin command); the -L option forces symbolic links
to be followed by resolving the link after processing instances
of .. in dir. If .. appears in dir, it is processed by removing
the immediately previous pathname component from dir, back to a
slash or the beginning of dir. If the -e option is supplied
with -P, and the current working directory cannot be success‐
fully determined after a successful directory change, cd will
return an unsuccessful status. On systems that support it, the
-# option presents the extended attributes associated with a
file as a directory. An argument of - is converted to $OLDPWD
before the directory change is attempted. If a non-empty direc‐
tory name from CDPATH is used, or if - is the first argument,
and the directory change is successful, the absolute pathname of
the new working directory is written to the standard output.
The return value is true if the directory was successfully
changed; false otherwise.
Look for the - argument about the seventh line from the end:
An argument of - is converted to $OLDPWD before the directory change is attempted.
Note that there is no -- argument - which seems to mean that it actually ignores it.
relevant excerpt from the bash man page covering usage of cd -
cd [-L|[-P [-e]] [-#]] [dir]
Change the current directory to dir.
...
An argument of -
is converted to $OLDPWD before the directory change is attempted. If a non-
empty directory name from CDPATH is used, or if - is the first argument, and
the directory change is successful, the absolute pathname of the new working
directory is written to the standard output. The return value is true if the
directory was successfully changed; false otherwise.

Create executable file for comand line Linux

When ever I log into Linux I usually go straight to the same folder i was wondering if in stead of typing in:
$cd Document/..../..../..../..../....
I could create an executable so I could just type ./csFolder and it would go straight there.
You can add a shell function in your .bashrc and restart your terminal:
csf() {
cd Document/..../..../..../..../....
}
Whenever you want to go to that directory, you just run csf.
Yo can do a symlink
ln -s /path/to/file /path/to/symlink
In addition to the other options (though if you use the function/alias option you want to use an absolute path to the target directory so it works from wherever you happen to be) you can use the environment variable CDPATH to help with this if you have a location you often go to from various other locations.
From the POSIX specification:
CDPATH
A -separated list of pathnames that refer to directories. The cd utility shall use this list in its attempt to change the directory, as described in the DESCRIPTION. An empty string in place of a directory pathname represents the current directory. If CDPATH is not set, it shall be treated as if it were an empty string.
Which means that if you set CDPATH to the parent of your target directory you can just use cd dirname from anywhere and go directly to the directory you wanted to be in.

Resources