One Command to Create a Directory and File Inside It Linux Command

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 file
  • somecommand > /path/to/file for a file containing the output of some command.

      eg: grep --help > randomtext.txt
    echo "This is some text" > randomtext.txt
  • nano /path/to/file or vi /path/to/file (or any 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
  1. There's no need to cd into a directory to create a file there.
  2. Don't parse the output of ls. The output of ls is only for looking at. Parsing it will break if your files or directories have names containing literal newlines or spaces.
  3. The pattern */ will match any directory in the current directory.
  4. 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



Leave a reply



Submit