When I use pwd it returns:
/home/ahmad/multifit-pretrain-lm/multifit/datasets
I look for a command so that I get the path relative to the home directory
multifit-pretrain-lm/multifit/datasets
I look for a command so that I get the path relative to the home directory
You can use bash parameter substitution here:
echo "${PWD#"$HOME"/}"
$PWD shell variable returns same value as the command pwd
${PWD#"$HOME"/} strips "$HOME"/ at the start from $PWD
Variable substitution is a good solution but since you asked for a command, here is one:
realpath --relative-base="$HOME" .
Related
I have a shell script copy_files.sh which I call once a day using a cron job.
However it has never worked I keep getting no such file or directory.
!#/bin/sh
for f in /home/site1/public_html/admin/data/*.csv
do
cp -v "$f" /home/site2/app/cron_jobs/data/"${f%.csv}".csv
done
I have checked via ssh that all paths are correct I have varified the path to /bin/sh using ls -l /bin/sh I have set perms and user and group to root for copy_files.sh I have disabled php open_basedir protection.
the shell script is in /home/site2/
Any ideas why I am still getting no such file or directory?
Is there anyway to check open_basedir protection is off that said considering the script is owned by root I don't see that being the problem unless it's executed as site2 user and not root?
Because of the way you use shell expansion, the variable in your for loop contains the absolute path to your files. Having the absolute path, means there is no need to use string manipulation (%) nor do you need to add the ".csv" to the filename, just get rid of it all together and provide the directory to which you're copying as your second argument to cp, see the example below.
#!/bin/sh
for f in /home/site1/public_html/admin/data/*.csv; do
cp -v "$f" /home/site2/app/cron_jobs/data
done
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.
I have these files and many others :
$ ls *
blue-3454534:
banana.txt file1.txt
green-junkjunk:
fever.txt vegan.txt
yellow-junkkkjunkkk:
funny.txt sunny.txt
I would like to run a command (loop) that would rename :
banana.txt to blue_banana.txt
file1.txt to blue_file1.txt
fever.txt to green_fever.txt
vegan.txt to green_vegan.txt
funny.txt to yellow_funny.txt
sunny.txt to yellow_sunny.txt
So as you understand it would put at the beginning (prefix) its parent directory name, until the dash (and ignore the remaining after the dash)
Ideally I would use mv or rename command, in a 1 command line loop.
Also, the renamed files should stay in the directory they are already.
Thank you for your help.
PS : there is no space nowhere nor in filename nor in directory name
Assuming that the name of the directory is stored in the variable subdir, and the name of the file (without directory part) is stored in the variable file, one way to rename it in the desired way is
cd $subdir
mv $file ${subdir%-*}_$file
cd -
The %-* part gets you the part of the directoryname before the dash.
This solution works for bash, zsh, and ksh.
For details, how the %- expansion works, see the excellent explanation in the bash manpage under the heading Parameter Expansion. For Zsh, it is in the man page called zshexpn and found under the heading PARAMETER EXPANSION (all uppercase). For ksh, the name of the section is simply Parameters.
Is there possibility of retrieving absolute path of symbolic file in bash? I cannot use realpath() and readlink() gives path of target. But i need absolute path of that symlink.
Is this an assignment? I can't imagine a real situation where you would not be allowed to use readlink but anyway, just for fun, here is a hack script to get the full path of the symlink provided as argument:
For the example I will use the symlink pidof which points to /bin/ usually, but is a symlink to /sbin/killall5 (Ubuntu)
#!/bin/bash
linkpath="$(which $1)"
line="$(ls -la $linkpath)"
realpath="$(echo $line | awk '{print $NF}')"
echo "Full path is: $realpath"
exit
Output:
user#system:~/Desktop$ ./test.sh pidof
Full path is: /sbin/killall5
user#system:~/Desktop$
The reason this works is because the output of ls shows the full path of symlinks. (At least in Debian distros)
I have several similarly structured directory trees.
something like:
~/
Tree1/
src/
bin/
Tree2/
src/
bin/
When I somewhere below Tree1/src I want to work with Tree1/bin. When I somewhere below Tree2/src I want to work with Tree2/bin.
Is there a way to define a shell variable whose value depends on my current working directory?
PWD is a variable already set to your current directory by bash, ksh and other shells.
cwd=$(pwd) should do the trick. It assigns the output of print working directory (pwd) to a variable.
To replace ~Tree1/src/dir1/dir2 with ~Tree1/bin you could do
bindir=$(pwd | sed 's/src.*/bin/')
See also Command Substitution
As jlliagre stated, bash (as many other modern shells) stores the current working directory in $PWD; if it is Tree1/src/some/other/directory, then you can extract "Tree1/bin" from it by just using "parameter expansion":
$ echo $PWD
Tree1/src/some/other/directory
$ echo ${PWD%%src*}bin
Tree1/bin
Generally $PWD variable (Present Working Directory) contains the path to the current directory. If this variable is not defined, you can use the pwd command that will return the current path.
Two other definitions of "current" include the directory you were in when the script was started (which is the value of start_dir="$PWD" at the start of the file, no matter where the script is) and the directory of the script itself - script_dir="$(dirname -- "$(readlink -f -- "$0")")".