One command to create a directory and file inside it linux command
mkdir B && touch B/myfile.txt
Alternatively, create a function:
mkfile() { mkdir -p -- "$1" && touch -- "$1"/"$2" }
Execute it with 2 arguments: path to create and filename. Saying:
mkfile B/C/D myfile.txt
would create the file myfile.txt
in the directory B/C/D
.
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 file along with nested directory in single command line
If I understand correctly and you simply want to be able to issue command foo/bar/baz/myfile.txt
(or something similar) and have the directories foo/bar/baz
created and a new file myfile.txt
created and opened in nano
all by that one command, then a short script is all you need, e.g.
Make it executable e.g. mv nanoopen.sh scriptname; chmod 0755 scriptname
, then just call ./scriptname
foo/bar/baz/file.txt
. If you put it in your path, you can skip the ./ too.
The easy way to put it in your path is to create a symlink to it in /usr/local/bin
which is generally in the default path.
So you could (sometime supersure is needed) ln -s /path/to/nanoopen.sh /usr/local/bin/scriptname
. Echo $PATH to confirm /usr/local/bin is in your path, then just use it like any program, scriptname arguments.
Or in some distros you can simply add it to /bin
folder with root access.
#!/bin/bash
[ -z "$1" ] && { ## validate one argument given
printf "error: insufficient input\nusage: %s filename\n" "${0##*/}"
exit 1
}
[ "$1" != "${1##*/}" ] && mkdir -p "${1%/*}" ## if it has directories, create
touch "$1" ## create the file
exec nano "$1" ## open in nano
Example Use/Output
$ bash nanoopen.sh foo/bar/baz/main.c
$ tree foo/
foo/
└── bar
└── baz
└── main.c
$ cat foo/bar/baz/main.c
My new source!
Unix - create path of folders and file
Use &&
to combine two commands in one shell line:
COMMAND1 && COMMAND2
mkdir -p /my/other/path/here/ && touch /my/other/path/here/cpedthing.txt
Note: Previously I recommended usage of ;
to separate the two commands but as pointed out by @trysis it's probably better to use &&
in most situations because in case COMMAND1
fails COMMAND2
won't be executed either. (Otherwise this might lead to issues you might not have been expecting.)
How to create a directory and give permission in single command
According to mkdir's man page...
mkdir -m 777 dirname
How to create a file in Linux from terminal window?
Depending on what you want the file to contain:
touch /path/to/file
for an empty filesomecommand > /path/to/file
for a file containing the output of some command.eg: grep --help > randomtext.txt
echo "This is some text" > randomtext.txtnano /path/to/file
orvi /path/to/file
(orany other editor emacs,gedit etc
)
It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn't exist
Linux shell: LOOP for create file in each folder
for dir in */; do
touch "$dir/test.txt"
done
- There's no need to
cd
into a directory to create a file there. - Don't parse the output of
ls
. The output ofls
is only for looking at. Parsing it will break if your files or directories have names containing literal newlines or spaces. - The pattern
*/
will match any directory in the current directory. - Quote your variable expansions. Your code would break if
IFS
is set to a digit.
If you really need to do a cd
into the directory, do it in a subshell. The changed working directory only affects the subshell and there is no need to cd
back.
for dir in */; do
( cd "$dir" && touch test.txt )
done
Make Nested Directories and Files with Linux
This assumes that you are using bash
, or some other shell that supports the {}
notation:
mkdir -p music/{rock/{punk,goth},classical/{baroque,early}}
Use all caps if you want, but it seems excessive.
--EDIT--
In the above, I had mistakenly thought that punk
, goth
, etc. were to be created as directories. If you want them to be files (with no content), you could do:
mkdir -p music/{rock,classical}
touch music/{rock/{punk,goth},classical/{baroque,early}}
It seems odd to bother with touch
, though. It would be better to delay creating the files until you actually provide content.
Related Topics
Attaching and Mounting Existing Ebs Volume to Ec2 Instance Filesystem Issue
How to Wait for Server Restart Using Ansible
How to Start a Shell Without Any User Configuration
Cmd 2>&1 > Log VS Cmd > Log 2>&1
What Does "Bash:No Job Control in This Shell" Mean
Shellscript to Monitor a Log File If Keyword Triggers Then Execute a Command
Error: Gdal-Config Not Found While Installing R Dependent Packages Whereas Gdal Is Installed
What Happens When You Run a Program
Hiding User Input on Terminal in Linux Script
How to Find Out What Group a Given User Has
Postgresql Database Default Location on Linux
How to Delete Many 0 Byte Files in Linux
Ubuntu - Run Command on Start-Up with "Sudo"
Count Occurrences of a Char in Plain Text File
Randomly Pick Lines from a File Without Slurping It with Unix
What Are Stalled-Cycles-Frontend and Stalled-Cycles-Backend in 'Perf Stat' Result