using touch to create directories?
Since find
outputs one file per line:
cat a.txt | while read file; do
if [[ "$file" = */* ]]; then
mkdir -p "${file%/*}";
fi;
touch "$file";
done
EDIT:
This would be slightly more efficient if the directories where created in a separate step:
cat a.txt | grep / | sed 's|/[^/]*$||' | sort -u | xargs -d $'\n' mkdir -p
cat a.txt | while read file; do
touch "$file";
done
And, no, touch
cannot create directories on its own.
is there a touch that can create parent directories like mkdir -p?
There is no touch that can create parent directory path, so write your own in standard POSIX-shell grammar that also works with zsh:
#!/usr/bin/env sh
touchp() {
for arg
do
# Get base directory
baseDir=${arg%/*}
# If whole path is not equal to the baseDire (sole element)
# AND baseDir is not a directory (or does not exist)
if ! { [ "$arg" = "$baseDir" ] || [ -d "$baseDir" ];}; then
# Creates leading directories
mkdir -p "${arg%/*}"
fi
# Touch file in-place without cd into dir
touch "$arg"
done
}
How to create file at specified directory with touch command
Try
touch ../my_directory/file
it won't create ../my_directory though, but I think this is not what you asked for.
Create directory and files with one command
I recommend -p
.
qc() { local p="$1";
if [[ -n "$p" ]];
then mkdir -p "$p" # can be any full or relative path;
else echo "Use: qc <dirpath> [f1[..fN]]"; return 1;
fi;
shift;
for f; do touch "$p/$f"; done;
}
$: qc
Use: qc <dirpath> [f1[..fN]]
$: cd /tmp
$: qc a/b/c 5 4 3 2 1 # relative path
$: qc a/b # no files; dir already exists; no problem
$: qc /tmp/a/b/c/d 3 2 1 # full path that partially exists
$: find a # all ok
a
a/b
a/b/c
a/b/c/1
a/b/c/2
a/b/c/3
a/b/c/4
a/b/c/5
a/b/c/d
a/b/c/d/1
a/b/c/d/2
a/b/c/d/3
Create a File with Touch on a specific Directory
Simply prepend the directory variable to the file you are touching.
touch "$MYDIR/$(basename $UNZIPFILE).done"
If the directory doesn't exist, you need to create it.
mkdir -p "$MYDIR" && touch "$MYDIR/$(basename $UNZIPFILE).done"
(It's also better to use $(command)
syntax instead of backticks for command substitution.)
How to touch a file and mkdir if needed in one line
In perl, using one of my favorite module: Path::Tiny.
path("/opt/test/test.txt")->touchpath;
From the doc:
Combines mkpath and touch. Creates the parent directory if it doesn't
exist, before touching the file.
Error when creating Directory and creating files using touch in UNIX
The issue is you missed the slash after livingthings in mkdir -p livingthings/{birds..... not mkdir -p livingthings{birds.... Because of this it creates a directory named livingthingsbirds instead of livingthings/birds. But then in your touch command u r using livingthings/ so thats why it cant find that directory.
Without changing your current directory, create an empty file in another directory in Linux?
Simply like that :
$ touch ../lastname/test2.txt
Note that instead of touch, you can use >
$ > ../lastname/test2.txt
You can also use an absolute path
$ > /Users/Directories/lastname/test2.txt
Related Topics
Create a Hard Link from a File Handle on Unix
How to Run a Windows Docker Container on Linux Host
Using Su/Sudo When Accessing Remote Git Repositories Over Ssh
How to Specify Which Version of Perl to Use on Centos
How to Install Google Test on Ubuntu Without Root Access
What Is The Equivalent of /Proc/Cpuinfo on Freebsd V8.1
Can a Pipe in Linux Ever Lose Data
Version Control System Which Keeps Track of Individual Files
How to Install Python Modules in a Docker Image
Sed Regex Problem on Mac, Works Fine on Linux
What Is The Maximum Allowed Depth of Sub-Folders
Trouble with Vagrant - "404 - Not Found"
How Can Beaglebone Black Be Used as Mass Storage Device
Rpm Installation Fails Due to Depencency /Bin/Sh Not Found
How to Connect to Amazon Linux Instance Using Remote Desktop from Windows 7