how to navigate between directories in absolute path - linux

I am working inside directory with absolute path of aa/bb/cc/dd/ee/ff/gg/hh/ii.
Now I want to jump to a directory dd. I know we can use cd ../.. multiple times to go back, but I want to know is there any faster way?
I am looking for the way something like CMD DIR(which is in PWD ) which will jump to DIR.
E.G. $ jump dd should result in terminal jumping to aa/bb/cc/dd.

You could write your own shell function that looks a bit like this:
up() {
local target dir
target="$1"
dir="$PWD"
while ! [ "$(basename "$dir")" = "$target" ]; do
dir="$(dirname "$dir")"
if [ "$dir" = "/" ]; then
>&2 echo "No such directory found"
return
fi
done
cd "$dir"
}
I have half-heartedly tested this in Bash and Zsh.
If you put this in a file called up.sh, you could source that file to get
access to this function. If you want to always have it, you should put it in the
appropriate rc file.
Here is some sample usage:
$ mkdir -p aa/bb/cc/dd/ee/ff/gg/hh/ii
$ cat up.sh
up() {
...
}
$ source up.sh
$ cd aa/bb/cc/dd/ee/ff/gg/hh/ii
$ up xyz
No such directory found
$ up bb
$ pwd
.../aa/bb

Related

linux cd to folder containing the file

Having a file path I would like to go to the directory of the file. A full path is like this:
/Users/user1/Documents/workspace/project/src/file.py
I would like in terminal to go the folder containing this file.
In this case it would be:
/Users/user1/Documents/workspace/project/src/
Right now I have to paste the file in terminal, delete the file name and do cd. Like:
cd /Users/user1/Documents/workspace/project/src/
Is there a command/ any way I can skip deleting the file and use a smart command to know it should go to the directory containing this file?
I have to examine daily a lot of files and deleting the filename from path everytime is becoming a very tedious task.
The desired action would be something like this:
>>smart_cd /path/to/file.py
>>pwd
/path/to
Assuming your are using Bash: You can put in your ~/.bashrc
function smart_cd() {
cd "$(dirname $1)"
}
then: (after re-loading your .bashrc)
$ smart_cd /Users/user1/Documents/workspace/project/src/file.py
Bonus: you can also replace your cd command to handle both normal and smart cd:
function cd() {
if [ $# -eq 0 ] ; then
# no arguments
builtin cd
elif [ -d $1 ] ; then
# argument is a directory
builtin cd "$1"
else
# argument is not a directory
builtin cd "$(dirname $1)"
fi
}
For more information see:
Special Parameters in man bash for $#
man test for explanation about [ ... -eq ... ] and [ -d ... ]

How do I get the absolute directory of a file in Bash?

I have written a Bash script that takes an input file as an argument and reads it.
This file contains some paths (relative to its location) to other files.
I would like the script to go to the folder containing the input file, to execute further commands.
In Linux, how do I get the folder (and just the folder) from an input file?
To get the full path use:
readlink -f relative/path/to/file
To get the directory of a file:
dirname relative/path/to/file
You can also combine the two:
dirname $(readlink -f relative/path/to/file)
If readlink -f is not available on your system you can use this*:
function myreadlink() {
(
cd "$(dirname $1)" # or cd "${1%/*}"
echo "$PWD/$(basename $1)" # or echo "$PWD/${1##*/}"
)
}
Note that if you only need to move to a directory of a file specified as a relative path, you don't need to know the absolute path, a relative path is perfectly legal, so just use:
cd $(dirname relative/path/to/file)
if you wish to go back (while the script is running) to the original path, use pushd instead of cd, and popd when you are done.
* While myreadlink above is good enough in the context of this question, it has some limitation relative to the readlink tool suggested above. For example it doesn't correctly follow a link to a file with different basename.
Take a look at realpath which is available on GNU/Linux, FreeBSD and NetBSD, but not OpenBSD 6.8. I use something like:
CONTAININGDIR=$(realpath ${FILEPATH%/*})
to do what it sounds like you're trying to do.
This will work for both file and folder:
absPath(){
if [[ -d "$1" ]]; then
cd "$1"
echo "$(pwd -P)"
else
cd "$(dirname "$1")"
echo "$(pwd -P)/$(basename "$1")"
fi
}
$cat abs.sh
#!/bin/bash
echo "$(cd "$(dirname "$1")"; pwd -P)"
Some explanations:
This script get relative path as argument "$1"
Then we get dirname part of that path (you can pass either dir or file to this script): dirname "$1"
Then we cd "$(dirname "$1"); into this relative dir
pwd -P and get absolute path. The -P option will avoid symlinks
As final step we echo it
Then run your script:
abs.sh your_file.txt
Try our new Bash library product realpath-lib over at GitHub that we have given to the community for free and unencumbered use. It's clean, simple and well documented so it's great to learn from. You can do:
get_realpath <absolute|relative|symlink|local file path>
This function is the core of the library:
if [[ -f "$1" ]]
then
# file *must* exist
if cd "$(echo "${1%/*}")" &>/dev/null
then
# file *may* not be local
# exception is ./file.ext
# try 'cd .; cd -;' *works!*
local tmppwd="$PWD"
cd - &>/dev/null
else
# file *must* be local
local tmppwd="$PWD"
fi
else
# file *cannot* exist
return 1 # failure
fi
# reassemble realpath
echo "$tmppwd"/"${1##*/}"
return 0 # success
}
It's Bash 4+, does not require any dependencies and also provides get_dirname, get_filename, get_stemname and validate_path.
Problem with the above answer comes with files input with "./" like "./my-file.txt"
Workaround (of many):
myfile="./somefile.txt"
FOLDER="$(dirname $(readlink -f "${ARG}"))"
echo ${FOLDER}
I have been using readlink -f works on linux
so
FULL_PATH=$(readlink -f filename)
DIR=$(dirname $FULL_PATH)
PWD=$(pwd)
cd $DIR
#<do more work>
cd $PWD

How to avoid cd to a directory without access permission

I have an array variable tht contains all directories in a folder.
I need to cd to each directory in the $array. bt when it reach to a non accessible directory , program halts . how can I avoid this so that my loop simply go to the next directory. code is
foreach dir ($array)
cd $dir
echo "directory is $dir"
cd - end
please help !!
Just test that the cd worked.
( if cd $dir 2> /dev/null; then
echo in directory $dir
# Do other things
fi )
Also, note the parentheses. These cause the entire clause to run in a subshell, so there's no need to cd back to the original location.
If iam not mistaken you can use something like
if [ -r ${dir} ]
then
echo "Have read access!"
fi
You can use -w for testing write access and -x for testing for the exceution bit.

How to get full path of a file?

Is there an easy way I can print the full path of file.txt ?
file.txt = /nfs/an/disks/jj/home/dir/file.txt
The <command>
dir> <command> file.txt
should print
/nfs/an/disks/jj/home/dir/file.txt
Use readlink:
readlink -f file.txt
I suppose you are using Linux.
I found a utility called realpath in coreutils 8.15.
realpath -s file.txt
/data/ail_data/transformed_binaries/coreutils/test_folder_realpath/file.txt
Since the question is about how to get the full/absolute path of a file and not about how to get the target of symlinks, use -s or --no-symlinks which means don't expand symlinks.
As per #styrofoam-fly and #arch-standton comments, realpath alone doesn't check for file existence, to solve this add the e argument: realpath -e file
The following usually does the trick:
echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")"
I know there's an easier way that this, but darned if I can find it...
jcomeau#intrepid:~$ python -c 'import os; print(os.path.abspath("cat.wav"))'
/home/jcomeau/cat.wav
jcomeau#intrepid:~$ ls $PWD/cat.wav
/home/jcomeau/cat.wav
On Windows:
Holding Shift and right clicking on a file in Windows Explorer gives you an option called Copy as Path.
This will copy the full path of the file to clipboard.
On Linux:
You can use the command realpath yourfile to get the full path of a file as suggested by others.
find $PWD -type f | grep "filename"
or
find $PWD -type f -name "*filename*"
If you are in the same directory as the file:
ls "`pwd`/file.txt"
Replace file.txt with your target filename.
I know that this is an old question now, but just to add to the information here:
The Linux command which can be used to find the filepath of a command file, i.e.
$ which ls
/bin/ls
There are some caveats to this; please see https://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/.
You could use the fpn (full path name) script:
% pwd
/Users/adamatan/bins/scripts/fpn
% ls
LICENSE README.md fpn.py
% fpn *
/Users/adamatan/bins/scripts/fpn/LICENSE
/Users/adamatan/bins/scripts/fpn/README.md
/Users/adamatan/bins/scripts/fpn/fpn.py
fpn is not a standard Linux package, but it's a free and open github project and you could set it up in a minute.
Works on Mac, Linux, *nix:
This will give you a quoted csv of all files in the current dir:
ls | xargs -I {} echo "$(pwd -P)/{}" | xargs | sed 's/ /","/g'
The output of this can be easily copied into a python list or any similar data structure.
echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")
This is explanation of what is going on at #ZeRemz's answer:
This script get relative path as argument "$1"
Then we get dirname part of that path (you can pass either dir or file to this script): dirname "$1"
Then we cd "$(dirname "$1") into this relative dir
&& pwd -P and get absolute path for it. -P option will avoid all symlinks
After that we append basename to absolute path: $(basename "$1")
As final step we echo it
You may use this function. If the file name is given without relative path, then it is assumed to be present in the current working directory:
abspath() { old=`pwd`;new=$(dirname "$1");if [ "$new" != "." ]; then cd $new; fi;file=`pwd`/$(basename "$1");cd $old;echo $file; }
Usage:
$ abspath file.txt
/I/am/in/present/dir/file.txt
Usage with relative path:
$ abspath ../../some/dir/some-file.txt
/I/am/in/some/dir/some-file.txt
With spaces in file name:
$ abspath "../../some/dir/another file.txt"
/I/am/in/some/dir/another file.txt
You can save this in your shell.rc or just put in console
function absolute_path { echo "$PWD/$1"; }
alias ap="absolute_path"
example:
ap somefile.txt
will output
/home/user/somefile.txt
I was surprised no one mentioned located.
If you have the locate package installed, you don't even need to be in the directory with the file of interest.
Say I am looking for the full pathname of a setenv.sh script. This is how to find it.
$ locate setenv.sh
/home/davis/progs/devpost_aws_disaster_response/python/setenv.sh
/home/davis/progs/devpost_aws_disaster_response/webapp/setenv.sh
/home/davis/progs/eb_testy/setenv.sh
Note, it finds three scripts in this case, but if I wanted just one I
would do this:
$ locate *testy*setenv.sh
/home/davis/progs/eb_testy/setenv.sh
This solution uses commands that exist on Ubuntu 22.04, but generally exist on most other Linux distributions, unless they are just to hardcore for s'mores.
The shortest way to get the full path of a file on Linux or Mac is to use the ls command and the PWD environment variable.
<0.o> touch afile
<0.o> pwd
/adir
<0.o> ls $PWD/afile
/adir/afile
You can do the same thing with a directory variable of your own, say d.
<0.o> touch afile
<0.o> d=/adir
<0.o> ls $d/afile
/adir/afile
Notice that without flags ls <FILE> and echo <FILE> are equivalent (for valid names of files in the current directory), so if you're using echo for that, you can use ls instead if you want.
If the situation is reversed, so that you have the full path and want the filename, just use the basename command.
<0.o> touch afile
<0.o> basename $PWD/afile
afile
In a similar scenario, I'm launching a cshell script from some other location. For setting the correct absolute path of the script so that it runs in the designated directory only, I'm using the following code:
set script_dir = `pwd`/`dirname $0`
$0 stores the exact string how the script was executed.
For e.g. if the script was launched like this: $> ../../test/test.csh,
$script_dir will contain /home/abc/sandbox/v1/../../test
For Mac OS X, I replaced the utilities that come with the operating system and replaced them with a newer version of coreutils. This allows you to access tools like readlink -f (for absolute path to files) and realpath (absolute path to directories) on your Mac.
The Homebrew version appends a 'G' (for GNU Tools) in front of the command name -- so the equivalents become greadlink -f FILE and grealpath DIRECTORY.
Instructions for how to install the coreutils/GNU Tools on Mac OS X through Homebrew can be found in this StackExchange arcticle.
NB: The readlink -f and realpath commands should work out of the box for non-Mac Unix users.
I like many of the answers already given, but I have found this really useful, especially within a script to get the full path of a file, including following symlinks and relative references such as . and ..
dirname `readlink -e relative/path/to/file`
Which will return the full path of the file from the root path onwards.
This can be used in a script so that the script knows which path it is running from, which is useful in a repository clone which could be located anywhere on a machine.
basePath=`dirname \`readlink -e $0\``
I can then use the ${basePath} variable in my scripts to directly reference other scripts.
Hope this helps,
Dave
This worked pretty well for me. It doesn't rely on the file system (a pro/con depending on need) so it'll be fast; and, it should be portable to most any *NIX. It does assume the passed string is indeed relative to the PWD and not some other directory.
function abspath () {
echo $1 | awk '\
# Root parent directory refs to the PWD for replacement below
/^\.\.\// { sub("^", "./") } \
# Replace the symbolic PWD refs with the absolute PWD \
/^\.\// { sub("^\.", ENVIRON["PWD"])} \
# Print absolute paths \
/^\// {print} \'
}
This is naive, but I had to make it to be POSIX compliant. Requires permission to cd into the file's directory.
#!/bin/sh
if [ ${#} = 0 ]; then
echo "Error: 0 args. need 1" >&2
exit 1
fi
if [ -d ${1} ]; then
# Directory
base=$( cd ${1}; echo ${PWD##*/} )
dir=$( cd ${1}; echo ${PWD%${base}} )
if [ ${dir} = / ]; then
parentPath=${dir}
else
parentPath=${dir%/}
fi
if [ -z ${base} ] || [ -z ${parentPath} ]; then
if [ -n ${1} ]; then
fullPath=$( cd ${1}; echo ${PWD} )
else
echo "Error: unsupported scenario 1" >&2
exit 1
fi
fi
elif [ ${1%/*} = ${1} ]; then
if [ -f ./${1} ]; then
# File in current directory
base=$( echo ${1##*/} )
parentPath=$( echo ${PWD} )
else
echo "Error: unsupported scenario 2" >&2
exit 1
fi
elif [ -f ${1} ] && [ -d ${1%/*} ]; then
# File in directory
base=$( echo ${1##*/} )
parentPath=$( cd ${1%/*}; echo ${PWD} )
else
echo "Error: not file or directory" >&2
exit 1
fi
if [ ${parentPath} = / ]; then
fullPath=${fullPath:-${parentPath}${base}}
fi
fullPath=${fullPath:-${parentPath}/${base}}
if [ ! -e ${fullPath} ]; then
echo "Error: does not exist" >&2
exit 1
fi
echo ${fullPath}
This works with both Linux and Mac OSX:
echo $(pwd)$/$(ls file.txt)
find / -samefile file.txt -print
Will find all the links to the file with the same inode number as file.txt
adding a -xdev flag will avoid find to cross device boundaries ("mount points"). (But this will probably cause nothing to be found if the find does not start at a directory on the same device as file.txt)
Do note that find can report multiple paths for a single filesystem object, because an Inode can be linked by more than one directory entry, possibly even using different names. For instance:
find /bin -samefile /bin/gunzip -ls
Will output:
12845178 4 -rwxr-xr-x 2 root root 2251 feb 9 2012 /bin/uncompress
12845178 4 -rwxr-xr-x 2 root root 2251 feb 9 2012 /bin/gunzip
Usually:
find `pwd` | grep <filename>
Alternatively, just for the current folder:
find `pwd` -maxdepth 1 | grep <filename>
This will work for both file and folder:
getAbsolutePath(){
[[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } ||
{ cd "$(dirname "$1")" || exit 1; echo "$(pwd -P)/$(basename "$1")"; }
}
Another Linux utility, that does this job:
fname <file>
For Mac OS, if you just want to get the path of a file in the finder, control click the file, and scroll down to "Services" at the bottom. You get many choices, including "copy path" and "copy full path". Clicking on one of these puts the path on the clipboard.
fp () {
PHYS_DIR=`pwd -P`
RESULT=$PHYS_DIR/$1
echo $RESULT | pbcopy
echo $RESULT
}
Copies the text to your clipboard and displays the text on the terminal window.
:)
(I copied some of the code from another stack overflow answer but cannot find that answer anymore)
In Mac OSX, do the following steps:
cd into the directory of the target file.
Type either of the following terminal commands.
Terminal
ls "`pwd`/file.txt"
echo $(pwd)/file.txt
Replace file.txt with your actual file name.
Press Enter
you#you:~/test$ ls
file
you#you:~/test$ path="`pwd`/`ls`"
you#you:~/test$ echo $path
/home/you/test/file
Beside "readlink -f" , another commonly used command:
$find /the/long/path/but/I/can/use/TAB/to/auto/it/to/ -name myfile
/the/long/path/but/I/can/use/TAB/to/auto/it/to/myfile
$
This also give the full path and file name at console
Off-topic: This method just gives relative links, not absolute. The readlink -f command is the right one.

Linux: copy and create destination dir if it does not exist

I want a command (or probably an option to cp) that creates the destination directory if it does not exist.
Example:
cp -? file /path/to/copy/file/to/is/very/deep/there
mkdir -p "$d" && cp file "$d"
(there's no such option for cp).
If both of the following are true:
You are using the GNU version of cp (and not, for instance, the Mac version), and
You are copying from some existing directory structure and you just need it recreated
then you can do this with the --parents flag of cp. From the info page (viewable at http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation or with info cp or man cp):
--parents
Form the name of each destination file by appending to the target
directory a slash and the specified name of the source file. The
last argument given to `cp' must be the name of an existing
directory. For example, the command:
cp --parents a/b/c existing_dir
copies the file `a/b/c' to `existing_dir/a/b/c', creating any
missing intermediate directories.
Example:
/tmp $ mkdir foo
/tmp $ mkdir foo/foo
/tmp $ touch foo/foo/foo.txt
/tmp $ mkdir bar
/tmp $ cp --parents foo/foo/foo.txt bar
/tmp $ ls bar/foo/foo
foo.txt
Short Answer
To copy myfile.txt to /foo/bar/myfile.txt, use:
mkdir -p /foo/bar && cp myfile.txt $_
How does this work?
There's a few components to this, so I'll cover all the syntax step by step.
The mkdir utility, as specified in the POSIX standard, makes directories. The -p argument, per the docs, will cause mkdir to
Create any missing intermediate pathname components
meaning that when calling mkdir -p /foo/bar, mkdir will create /foo and /foo/bar if /foo doesn't already exist. (Without -p, it will instead throw an error.
The && list operator, as documented in the POSIX standard (or the Bash manual if you prefer), has the effect that cp myfile.txt $_ only gets executed if mkdir -p /foo/bar executes successfully. This means the cp command won't try to execute if mkdir fails for one of the many reasons it might fail.
Finally, the $_ we pass as the second argument to cp is a "special parameter" which can be handy for avoiding repeating long arguments (like file paths) without having to store them in a variable. Per the Bash manual, it:
expands to the last argument to the previous command
In this case, that's the /foo/bar we passed to mkdir. So the cp command expands to cp myfile.txt /foo/bar, which copies myfile.txt into the newly created /foo/bar directory.
Note that $_ is not part of the POSIX standard, so theoretically a Unix variant might have a shell that doesn't support this construct. However, I don't know of any modern shells that don't support $_; certainly Bash, Dash, and zsh all do.
A final note: the command I've given at the start of this answer assumes that your directory names don't have spaces in. If you're dealing with names with spaces, you'll need to quote them so that the different words aren't treated as different arguments to mkdir or cp. So your command would actually look like:
mkdir -p "/my directory/name with/spaces" && cp "my filename with spaces.txt" "$_"
Such an old question, but maybe I can propose an alternative solution.
You can use the install programme to copy your file and create the destination path "on the fly".
install -D file /path/to/copy/file/to/is/very/deep/there/file
There are some aspects to take in consideration, though:
you need to specify also the destination file name, not only the destination path
the destination file will be executable (at least, as far as I saw from my tests)
You can easily amend the #2 by adding the -m option to set permissions on the destination file (example: -m 664 will create the destination file with permissions rw-rw-r--, just like creating a new file with touch).
And here it is the shameless link to the answer I was inspired by =)
Shell function that does what you want, calling it a "bury" copy because it digs a hole for the file to live in:
bury_copy() { mkdir -p `dirname $2` && cp "$1" "$2"; }
Here's one way to do it:
mkdir -p `dirname /path/to/copy/file/to/is/very/deep/there` \
&& cp -r file /path/to/copy/file/to/is/very/deep/there
dirname will give you the parent of the destination directory or file. mkdir -p `dirname ...` will then create that directory ensuring that when you call cp -r the correct base directory is in place.
The advantage of this over --parents is that it works for the case where the last element in the destination path is a filename.
And it'll work on OS X.
with all my respect for answers above, I prefer to use rsync as follow:
$ rsync -a directory_name /path_where_to_inject_your_directory/
example:
$ rsync -a test /usr/local/lib/
install -D file -m 644 -t /path/to/copy/file/to/is/very/deep/there
This does it for me
cp -vaR ./from ./to
This is very late but it may help a rookie somewhere. If you need to AUTO create folders rsync should be your best friend.
rsync /path/to/sourcefile /path/to/tragetdir/thatdoestexist/
Simply add the following in your .bashrc, tweak if you need. Works in Ubuntu.
mkcp() {
test -d "$2" || mkdir -p "$2"
cp -r "$1" "$2"
}
E.g
If you want to copy 'test' file to destination directory 'd'
Use,
mkcp test a/b/c/d
mkcp will first check if destination directory exists or not, if not then make it and copy source file/directory.
Just to resume and give a complete working solution, in one line.
Be careful if you want to rename your file, you should include a way to provide a clean dir path to mkdir. $fdst can be file or dir.
Next code should work in any case.
fsrc=/tmp/myfile.unk
fdst=/tmp/dir1/dir2/dir3/myfile.txt
mkdir -p $(dirname ${fdst}) && cp -p ${fsrc} ${fdst}
or bash specific
fsrc=/tmp/myfile.unk
fdst=/tmp/dir1/dir2/dir3/myfile.txt
mkdir -p ${fdst%/*} && cp -p ${fsrc} ${fdst}
As suggested above by help_asap and spongeman you can use the 'install' command to copy files to existing directories or create create new destination directories if they don't already exist.
Option 1
install -D filename some/deep/directory/filename
copies file to a new or existing directory and gives filename default 755 permissions
Option 2
install -D filename -m640 some/deep/directory/filename
as per Option 1 but gives filename 640 permissions.
Option 3
install -D filename -m640 -t some/deep/directory/
as per Option 2 but targets filename into target directory so filename does not need to be written in both source and target.
Option 4
install -D filena* -m640 -t some/deep/directory/
as per Option 3 but uses a wildcard for multiple files.
It works nicely in Ubuntu and combines two steps (directory creation then file copy) into one single step.
Simply without creating script and with simple command ...
mkdir -p /destination-folder/ && cp file-name /destination-folder/
I wrote a support script for cp, called CP (note capital letters) that's intended to do exactly this. Script will check for errors in the path you've put in (except the last one which is the destination) and if all is well, it will do an mkdir -p step to create the destination path before starting the copy. At this point the regular cp utility takes over and any switches you use with CP (like -r, -p, -rpL gets piped directly to cp). Before you use my script, there are a few things you need to understand.
all the info here can be accessed by doing CP --help. CP --help-all include's cp's switches.
regular cp won't do the copy if it doesn't find the destination path. You don't have such a safety net for typos with CP. You're destination will be created, so if you misspell your destination as /usrr/share/icons or /usr/share/icon well that's what's going to be created.
regular cp tends to model it's behavior on the existing path: cp /a/b /c/d will vary on whether d exists or not. if d is an existing folder, cp will copy b into it, making /c/d/b. If d doesn't exist, b will be copied into c and renamed to d. If d exists but is a file and b is a file, it will be overwritten by b's copy. If c doesn't exist, cp doesn't do the copy and exits.
CP doesn't have the luxury of taking cues from existing paths, so it has to have some very firm behavior patterns. CP assumes that the item you're copying is being dropped in the destination path and is not the destination itself (aka, a renamed copy of the source file/folder). Meaning:
"CP /a/b /c/d" will result in /c/d/b if d is a folder
"CP /a/b /c/b" will result in /c/b/b if b in /c/b is a folder.
If both b and d are files: CP /a/b /c/d will result in /c/d (where d is a copy of b). Same for CP /a/b /c/b in the same circumstance.
This default CP behavior can be changed with the "--rename" switch. In this case, it's assumed that
"CP --rename /a/b /c/d" is copying b into /c and renaming the copy to d.
A few closing notes: Like with cp, CP can copy multiple items at a time with the last path being listed assumed to be the destination. It can also handle paths with spaces as long as you use quotation marks.
CP will check the paths you put in and make sure they exist before doing the copy. In strict mode (available through --strict switch), all files/folders being copied must exist or no copy takes place. In relaxed mode (--relaxed), copy will continue if at least one of the items you listed exists. Relaxed mode is the default, you can change the mode temporarily via the switches or permanently by setting the variable easy_going at the beginning of the script.
Here's how to install it:
In a non-root terminal, do:
sudo echo > /usr/bin/CP; sudo chmod +x /usr/bin/CP; sudo touch /usr/bin/CP
gedit admin:///usr/bin/CP
In gedit, paste CP utility and save:
#!/bin/bash
#Regular cp works with the assumption that the destination path exists and if it doesn't, it will verify that it's parent directory does.
#eg: cp /a/b /c/d will give /c/d/b if folder path /c/d already exists but will give /c/d (where d is renamed copy of b) if /c/d doesn't exists but /c does.
#CP works differently, provided that d in /c/d isn't an existing file, it assumes that you're copying item into a folder path called /c/d and will create it if it doesn't exist. so CP /a/b /c/d will always give /c/d/b unless d is an existing file. If you put the --rename switch, it will assume that you're copying into /c and renaming the singl item you're copying from b to d at the destination. Again, if /c doesn't exist, it will be created. So CP --rename /a/b /c/d will give a /c/d and if there already a folder called /c/d, contents of b will be merged into d.
#cp+ $source $destination
#mkdir -p /foo/bar && cp myfile "$_"
err=0 # error count
i=0 #item counter, doesn't include destination (starts at 1, ex. item1, item2 etc)
m=0 #cp switch counter (starts at 1, switch 1, switch2, etc)
n=1 # argument counter (aka the arguments inputed into script, those include both switches and items, aka: $1 $2 $3 $4 $5)
count_s=0
count_i=0
easy_going=true #determines how you deal with bad pathes in your copy, true will allow copy to continue provided one of the items being copied exists, false will exit script for one bad path. this setting can also be changed via the custom switches: --strict and --not-strict
verbal="-v"
help="===============================================================================\
\n CREATIVE COPY SCRIPT (CP) -- written by thebunnyrules\
\n===============================================================================\n
\n This script (CP, note capital letters) is intended to supplement \
\n your system's regular cp command (note uncapped letters). \n
\n Script's function is to check if the destination path exists \
\n before starting the copy. If it doesn't it will be created.\n
\n To make this happen, CP assumes that the item you're copying is \
\n being dropped in the destination path and is not the destination\
\n itself (aka, a renamed copy of the source file/folder). Meaning:\n
\n * \"CP /a/b /c/d\" will result in /c/d/b \
\n * even if you write \"CP /a/b /c/b\", CP will create the path /a/b, \
\n resulting in /c/b/b. \n
\n Of course, if /c/b or /c/d are existing files and /a/b is also a\
\n file, the existing destination file will simply be overwritten. \
\n This behavior can be changed with the \"--rename\" switch. In this\
\n case, it's assumed that \"CP --rename /a/b /c/d\" is copying b into /c \
\n and renaming the copy to d.\n
\n===============================================================================\
\n CP specific help: Switches and their Usages \
\n===============================================================================\n
\
\n --rename\tSee above. Ignored if copying more than one item. \n
\n --quiet\tCP is verbose by default. This quiets it.\n
\n --strict\tIf one+ of your files was not found, CP exits if\
\n\t\tyou use --rename switch with multiple items, CP \
\n\t\texits.\n
\n --relaxed\tIgnores bad paths unless they're all bad but warns\
\n\t\tyou about them. Ignores in-appropriate rename switch\
\n\t\twithout exiting. This is default behavior. You can \
\n\t\tmake strict the default behavior by editing the \
\n\t\tCP script and setting: \n
\n\t\teasy_going=false.\n
\n --help-all\tShows help specific to cp (in addition to CP)."
cp_hlp="\n\nRegular cp command's switches will still work when using CP.\
\nHere is the help out of the original cp command... \
\n\n===============================================================================\
\n cp specific help: \
\n===============================================================================\n"
outro1="\n******************************************************************************\
\n******************************************************************************\
\n******************************************************************************\
\n USE THIS SCRIPT WITH CARE, TYPOS WILL GIVE YOU PROBLEMS...\
\n******************************************************************************\
\n******************************* HIT q TO EXIT ********************************\
\n******************************************************************************"
#count and classify arguments that were inputed into script, output help message if needed
while true; do
eval input="\$$n"
in_=${input::1}
if [ -z "$input" -a $n = 1 ]; then input="--help"; fi
if [ "$input" = "-h" -o "$input" = "--help" -o "$input" = "-?" -o "$input" = "--help-all" ]; then
if [ "$input" = "--help-all" ]; then
echo -e "$help"$cp_hlp > /tmp/cp.hlp
cp --help >> /tmp/cp.hlp
echo -e "$outro1" >> /tmp/cp.hlp
cat /tmp/cp.hlp|less
cat /tmp/cp.hlp
rm /tmp/cp.hlp
else
echo -e "$help" "$outro1"|less
echo -e "$help" "$outro1"
fi
exit
fi
if [ -z "$input" ]; then
count_i=$(expr $count_i - 1 ) # remember, last item is destination and it's not included in cound
break
elif [ "$in_" = "-" ]; then
count_s=$(expr $count_s + 1 )
else
count_i=$(expr $count_i + 1 )
fi
n=$(expr $n + 1)
done
#error condition: no items to copy or no destination
if [ $count_i -lt 0 ]; then
echo "Error: You haven't listed any items for copying. Exiting." # you didn't put any items for copying
elif [ $count_i -lt 1 ]; then
echo "Error: Copying usually involves a destination. Exiting." # you put one item and no destination
fi
#reset the counter and grab content of arguments, aka: switches and item paths
n=1
while true; do
eval input="\$$n" #input=$1,$2,$3,etc...
in_=${input::1} #first letter of $input
if [ "$in_" = "-" ]; then
if [ "$input" = "--rename" ]; then
rename=true #my custom switches
elif [ "$input" = "--strict" ]; then
easy_going=false #exit script if even one of the non-destinations item is not found
elif [ "$input" = "--relaxed" ]; then
easy_going=true #continue script if at least one of the non-destination items is found
elif [ "$input" = "--quiet" ]; then
verbal=""
else
#m=$(expr $m + 1);eval switch$m="$input" #input is a switch, if it's not one of the above, assume it belongs to cp.
switch_list="$switch_list \"$input\""
fi
elif ! [ -z "$input" ]; then #if it's not a switch and input is not empty, it's a path
i=$(expr $i + 1)
if [ ! -f "$input" -a ! -d "$input" -a "$i" -le "$count_i" ]; then
err=$(expr $err + 1 ); error_list="$error_list\npath does not exit: \"b\""
else
if [ "$i" -le "$count_i" ]; then
eval item$i="$input"
item_list="$item_list \"$input\""
else
destination="$input" #destination is last items entered
fi
fi
else
i=0
m=0
n=1
break
fi
n=$(expr $n + 1)
done
#error condition: some or all item(s) being copied don't exist. easy_going: continue if at least one item exists, warn about rest, not easy_going: exit.
#echo "err=$err count_i=$count_i"
if [ "$easy_going" != true -a $err -gt 0 -a $err != $count_i ]; then
echo "Some of the paths you entered are incorrect. Script is running in strict mode and will therefore exit."
echo -e "Bad Paths: $err $error_list"
exit
fi
if [ $err = $count_i ]; then
echo "ALL THE PATHS you have entered are incorrect! Exiting."
echo -e "Bad Paths: $err $error_list"
fi
#one item to one destination:
#------------------------------
#assumes that destination is folder, it does't exist, it will create it. (so copying /a/b/c/d/firefox to /e/f/firefox will result in /e/f/firefox/firefox
#if -rename switch is given, will assume that the top element of destination path is the new name for the the item being given.
#multi-item to single destination:
#------------------------------
#assumes destination is a folder, gives error if it exists and it's a file. -rename switch will be ignored.
#ERROR CONDITIONS:
# - multiple items being sent to a destination and it's a file.
# - if -rename switch was given and multiple items are being copied, rename switch will be ignored (easy_going). if not easy_going, exit.
# - rename option but source is folder, destination is file, exit.
# - rename option but source is file and destination is folder. easy_going: option ignored.
if [ -f "$destination" ]; then
if [ $count_i -gt 1 ]; then
echo "Error: You've selected a single file as a destination and are copying multiple items to it. Exiting."; exit
elif [ -d "$item1" ]; then
echo "Error: Your destination is a file but your source is a folder. Exiting."; exit
fi
fi
if [ "$rename" = true ]; then
if [ $count_i -gt 1 ]; then
if [ $easy_going = true ]; then
echo "Warning: you choose the rename option but are copying multiple items. Ignoring Rename option. Continuing."
else
echo "Error: you choose the rename option but are copying multiple items. Script running in strict mode. Exiting."; exit
fi
elif [ -d "$destination" -a -f "$item1" ]; then
echo -n "Warning: you choose the rename option but source is a file and destination is a folder with the same name. "
if [ $easy_going = true ]; then
echo "Ignoring Rename option. Continuing."
else
echo "Script running in strict mode. Exiting."; exit
fi
else
dest_jr=$(dirname "$destination")
if [ -d "$destination" ]; then item_list="$item1/*";fi
mkdir -p "$dest_jr"
fi
else
mkdir -p "$destination"
fi
eval cp $switch_list $verbal $item_list "$destination"
cp_err="$?"
if [ "$cp_err" != 0 ]; then
echo -e "Something went wrong with the copy operation. \nExit Status: $cp_err"
else
echo "Copy operation exited with no errors."
fi
exit
cp has multiple usages:
$ cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
or: cp [OPTION]... SOURCE... DIRECTORY
or: cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
#AndyRoss's answer works for the
cp SOURCE DEST
style of cp, but does the wrong thing if you use the
cp SOURCE... DIRECTORY/
style of cp.
I think that "DEST" is ambiguous without a trailing slash in this usage (i.e. where the target directory doesn't yet exist), which is perhaps why cp has never added an option for this.
So here's my version of this function which enforces a trailing slash on the dest dir:
cp-p() {
last=${#: -1}
if [[ $# -ge 2 && "$last" == */ ]] ; then
# cp SOURCE... DEST/
mkdir -p "$last" && cp "$#"
else
echo "cp-p: (copy, creating parent dirs)"
echo "cp-p: Usage: cp-p SOURCE... DEST/"
fi
}
i strongly suggest ditto.
just works.
ditto my/location/poop.txt this/doesnt/exist/yet/poop.txt
Just had the same issue. My approach was to just tar the files into an archive like so:
tar cf your_archive.tar file1 /path/to/file2 path/to/even/deeper/file3
tar automatically stores the files in the appropriate structure within the archive. If you run
tar xf your_archive.tar
the files are extracted into the desired directory structure.
Copy from source to an non existing path
mkdir –p /destination && cp –r /source/ $_
NOTE: this command copies all the files
cp –r for copying all folders and its content
$_ work as destination which is created in last command
Oneliner to create a small script that can be used as subcommand, in find for instance:
set +H; echo -e "#!/bin/sh\nmkdir -p \$(dirname \"\$2\"); cp \"\$1\" \"$2\"\;" > ~/local/bin/cpmkdir; chmod +x ~/local/bin/cpmkdir
You can then use it like:
find -name files_you_re_lookin_for.* -exec cpmkdir {} ../extracted_copy/{} \;
rsync file /path/to/copy/file/to/is/very/deep/there
This might work, if you have the right kind of rsync.
You can use find with Perl. Command will be like this:
find file | perl -lne '$t = "/path/to/copy/file/to/is/very/deep/there/"; /^(.+)\/.+$/; `mkdir -p $t$1` unless(-d "$t$1"); `cp $_ $t$_` unless(-f "$t$_");'
This command will create directory $t if it doesn't exist. And than copy file into $t only unless file exists inside $t.
This works on GNU /bin/bash version 3.2 on MacOS (tested on both Catalina and Big Sur)
cp -Rv <existing-source-folder>/ <non-existing-2becreated-destination-folder>
the "v" option is for verbose.
And I think of the "-R" option as "Recursive".
man's full description of -R is:
If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. If the source_file ends in a /, the contents of the directory are copied rather than the directory itself. This option also causes symbolic links to be copied, rather than indirected through, and for cp to create special files rather than copying them as normal files. Created directories have the same mode as the corresponding source directory, unmodified by the process' umask.
In -R mode, cp will continue copying even if errors are detected.
Note that cp copies hard-linked files as separate files. If you need to preserve hard links, consider using tar(1), cpio(1), or pax(1) instead.
In the example below, I'm using a "/" at the end of existingfolder so that it copies all the contents of existingfolder (and not the folder itself) into newfolder:
cp -Rv existingfolder/ newfolder
Try it.
Only for macOS
rsync -R <source file path> destination_folder
For macOS --parents option of cp doesn't work
Many of the other solutions don't work on files or folders which need escaping. Here is a solution which works for files and folders, and escapes spaces and other special characters. Tested in a busybox ash shell which doesn't have access to some of the fancier options.
export file="annoying folder/bar.txt"
export new_parent="/tmp/"
# Creates /tmp/annoying folder/
mkdir -p "$(dirname "$new_folder/$file")"
# Copies file to /tmp/annoying folder/bar.txt
cp -r "$file" "$new_folder/$file"
This should also work if you omit bar.txt if you need the recursive copy of a whole folder.
Let's say you are doing something like
cp file1.txt A/B/C/D/file.txt
where A/B/C/D are directories which do not exist yet
A possible solution is as follows
DIR=$(dirname A/B/C/D/file.txt)
# DIR= "A/B/C/D"
mkdir -p $DIR
cp file1.txt A/B/C/D/file.txt
hope that helps!
Simple
cp -a * /path/to/dst/
should do the trick.

Resources