Linux: Copy and Create Destination Dir If It Does Not Exist

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

mkdir -p "$d" && cp file "$d"

(there's no such option for cp).

Copy files to destination and create destination if does not exist

You assume that the last argument must be a directory, and if no such directory exists, it should be created. In zsh, I would do it like this:

mkcp() {
local destdir=$@[-1]
if [[ -f $destdir ]]
then
echo Missing destination directory 2>&1
return 1
else
mkdir -p $destdir
if [[ -d $destdir ]]
then
cp "$@"
else
echo Can not create $destdir
return 1
fi
fi
}

The problem is not the copying (the plain cp command can do it and no loop is needed), so the focus here is on error checking.

Note that with my approach, switches can be passed implicitly to cp, for instance

mkcp -r foo bar baz

copies recursively the subdirectories too, and

mkcp -rv foo bar baz

is in addition printing the names of the files which are copied.

Is there anyway to copy a file to a directory that does not exist?

before your copy, you need to perform

mkdir -p ./createThisFolder

If you want to know whether or not there are files in the directory, I'd recommend directly checking whether or not your files exist (instead of checking if the directory exists). For instance:

if [ ! -s ./createThisFolder/temp.file ] ; then 
echo "file doesn't exist (or it is empty)"
fi

Create path when copying a file in linux

If you have Gnu coreutils (normally you will on linux), you can use:

install -m 0644 -D source_file dest_filename

which automatically creates necessary directories.

The -m 0644 flag sets the permissions of the new file to 0644 (rw-r--r--); you need that because the default is to set the permissions to rxwr-xr-x (on the assumption that the file should be executable, since that's the usual use case) rather than copying the permissions of the original file.

Copy files to their corresponding destination folders

Seems that this Bash script can do the job

#!/usr/bin/env bash
for file in $(<list.txt); do
dirn="${file%.*}"
mkdir -p "$dirn"
cp "$file" "$dirn"
done

Create if non exist directories with year name and subdirectories having month and day date

It's a good idea to indent your code consistently. It makes it easier to spot errors.

Your if statements do nothing when a test fails. You need to add code to handle the else cases:

if [ ! -d "$YEAR" ]; then
mkdir $YEAR
cd $YEAR
if [ ! -d "$MONTH" ]; then
mkdir $MONTH
cd $MONTH
if [ ! -d "$DAY" ]; then
mkdir $DAY
else
: # do this if DAY directory exists
fi
else
: # do this if MONTH directory exists
fi
else
: # do this if YEAR directory exists
fi

Alternatively, in this case it is cleaner to unroll into separate tests:

if [ ! -d "$YEAR" ]; then
mkdir $YEAR
fi
cd $YEAR
if [ ! -d "$MONTH" ]; then
mkdir $MONTH
fi
cd $MONTH
if [ ! -d "$DAY" ]; then
mkdir $DAY
fi
cd ../..

However, it is even simpler to just let mkdir do the work for you with the -p option which creates any directories that are needed:

mkdir -p "$YEAR/$MONTH/$DAY"

# or: mkdir -p "$(date +%Y/%m/%d)"

Is there a way to make mv create the directory to be moved to if it doesn't exist?

How about this one-liner (in bash):

mkdir --parents ./some/path/; mv yourfile.txt $_

Breaking that down:

mkdir --parents ./some/path
# if it doesn't work; try
mkdir -p ./some/path

creates the directory (including all intermediate directories), after which:

mv yourfile.txt $_

moves the file to that directory ($_ expands to the last argument passed to the previous shell command, ie: the newly created directory).

I am not sure how far this will work in other shells, but it might give you some ideas about what to look for.

Here is an example using this technique:

$ > ls
$ > touch yourfile.txt
$ > ls
yourfile.txt
$ > mkdir --parents ./some/path/; mv yourfile.txt $_
$ > ls -F
some/
$ > ls some/path/
yourfile.txt


Related Topics



Leave a reply



Submit