I am using the following 2 commands to create a 0B file and set its extn to 644
touch filename.ext
chmod 777 filename.txt
My question is that w
For bash
, simply use chmod
with file redirection and history expansion:
chmod 777 filename.txt>>!#:2
For ksh
and zsh
you have to drop the history expansion (as shown above, there may be other ways) and use:
chmod 644 filename>>filename
For scripts of any shell you don't have (and really don't need) history expansion so use the above there as well.
install -m 777 /dev/null filename.txt
The only reason your files are not created with 666 permissions right off of the bat is the umask. So if you disable the umask:
umask 0
then when you touch the file it will end up with permissions 666 automatically. It would not be a good idea to make text files executable usually. Directories would end up with 777 permission with umask disabled.
chicks@freecandy /tmp $ umask
0022
chicks@freecandy /tmp $ touch x
chicks@freecandy /tmp $ mkdir xx
chicks@freecandy /tmp $ umask 0
chicks@freecandy /tmp $ touch y
chicks@freecandy /tmp $ mkdir yy
chicks@freecandy /tmp $ ls -ld x xx y yy
-rw-r--r-- 1 chicks chicks 484 Jan 24 14:37 x
drwxr-xr-x 2 chicks chicks 4096 Jan 24 14:37 xx
-rw-rw-rw- 1 chicks chicks 0 Jan 24 14:37 y
drwxrwxrwx 2 chicks chicks 4096 Jan 24 14:37 yy
Because of Docker and Alpine, I imagine there are a lot of people interested in creating executable one-liners without the help of bash-isms. It's pretty simple with install
$ docker build -t temp - <<EOF
FROM alpine
RUN echo "date" | install -m 775 /dev/stdin /bin/now
CMD /bin/now
EOF
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM alpine
---> d6e46aa2470d
Step 2/3 : RUN echo "date" | install -m 775 /dev/stdin /bin/now
---> Running in 95919b575638
Removing intermediate container 95919b575638
---> cd1fafd96ef3
Step 3/3 : CMD /bin/now
---> Running in 03b5c3ac7265
Removing intermediate container 03b5c3ac7265
---> 8f30b527dd29
Successfully built 8f30b527dd29
Successfully tagged temp:latest
$ docker run --rm temp
Thu Nov 12 07:44:24 UTC 2020
$ docker run --rm temp ls -la /bin/now
-rwxrwxr-x 1 root root 5 Nov 12 07:44 /bin/now
$ docker rmi temp
Untagged: temp:latest
Deleted: sha256:8f30b527dd29203b67c86290b34b0a29d3c98a48134609d0b1e2b89087a7d6e7
Deleted: sha256:cd1fafd96ef3084226c5f98f472e3e08bc9a9f0448cc3e68b6f1c192d12d778a
Deleted: sha256:e58c75acbf953a64d35089cf50e1c5b762601e1cb9d9d1d668e135f23ce1fe86
touch filename.ext && chmod 777 $_
$_ is the most recent parameter
but as others have said 777 isn't a good idea
You can create your own command:
create () {
touch "$1"
chmod "$2" "$1"
}
create filename.ext 644