Commands using chmod (95)

  • The above command will set the GID bit on all directories named .svn in the current directory recursively. This makes the group ownership of all .svn folders be the group ownership for all files created in that folder, no matter the user. This is useful for me as the subversion working directory on my server is also the live website and needs to be auto committed to subversion every so often via cron as well as worked on by multiple users. Setting the GID bit on the .svn folders makes sure we don't have a mix of .svn metadata created by a slew of different users.


    1
    find . -type d -name .svn -exec chmod g+s "{}" \;
    mitzip · 2010-04-27 16:51:00 21
  • This one liner; combines all sequentially numbered files; in this example IMG_0001.png to IMG_1121.png by generating the shell script, making the shell script executable and then running the shell script to combine the 1121 png into a single png file named _final.png tested on Mac OS X 10.6.3 with ImageMagick 6.5.8-0 2009-11-22 Q16 http://www.imagemagick.org


    1
    echo -n "convert " > itcombino.sh; printf "IMG_%00004u.png " {1..1121} >> itcombino.sh; echo -n "-layers merge _final.png" >> itcombino.sh; chmod +x itcombino.sh && ./itcombino.sh
    IsraelTorres · 2010-05-22 03:56:30 5
  • The first argument is the interpreter for your script, the second argument is the name of the script to create. Show Sample Output


    1
    shebang() { if i=$(which $1); then printf '#!%s\n\n' $i > $2 && vim + $2 && chmod 755 $2; else echo "'which' could not find $1, is it in your \$PATH?"; fi; }
    bartonski · 2011-03-09 14:47:32 25
  • This will create, in the current directory, a file called 'pk.pem' containing an unencrypted 2048-bit RSA private key and a file called 'cert.pem' containing a certificate signed by 'pk.pem'. The private key file will have mode 600. !!ATTENTION!! ==> this command will overwrite both files if present.


    1
    touch pk.pem && chmod 600 pk.pem && openssl genrsa -out pk.pem 2048 && openssl req -new -batch -key pk.pem | openssl x509 -req -days 365 -signkey pk.pem -out cert.pem
    bfreis · 2011-05-11 18:09:33 6
  • This command is useful to recursively make executable all "*.sh" files in a folder. This command is useful to apply chmod recursively in a determined kind of file.


    1
    find ./ -name "*.sh" -exec chmod +x {} \;
    Renato · 2013-02-25 17:14:55 8

  • 1
    inotifywait -mr -e CREATE $HOME/bin/ | while read i; do chmod +x "$(echo "$i" | sed 's/ \S* //')"; done
    Sepero · 2014-03-08 19:21:53 6

  • 1
    function fixperms() { chmod -R a=r,u+w,a+X . }
    ateijelo · 2015-12-24 16:59:27 11
  • Create a bash script to change the modification time for each file in 'files.txt' such that they are in the same order as in 'files.txt' File name for bash script specified by variable, 'scriptName'. It is made an executable once writing into it has been completed. Show Sample Output


    1
    scriptName="reorder_files.sh"; echo -e '#!/bin/sh\n' > "${scriptName}"; cat files.txt | while read file; do echo "touch ${file}; sleep 0.5;" >> "${scriptName}"; done; chmod +x "${scriptName}";
    programmer · 2016-04-19 11:52:00 15
  • This function will encrypt a bash script and will only execute it after providing the passphrase. Requires mcrypt to be installed on the system. Show Sample Output


    1
    scrypt(){ [ -n "$1" ]&&{ echo '. <(echo "$(tail -n+2 $0|base64 -d|mcrypt -dq)"); exit;'>$1.scrypt;cat $1|mcrypt|base64 >>$1.scrypt;chmod +x $1.scrypt;};}
    rodolfoap · 2017-06-14 16:27:20 20
  • changes group ownership of all files/dirs in /path/to/dir to a project group [projgroup] and then gives the sgid bit to directories in that tree - all subsequently created files will inherit [projgroup]'s gid.


    0
    chgrp -R [projgroup] ; find /path/to/dir -type d -exec chmod g+s {} \;
    wwest4 · 2009-02-05 16:49:24 17

  • 0
    find . -iname "*wav" > step1 ; sed -e 's/\(^.*\)wav/\"\1wav\" \"\1mp3\"/' step1 > step2 ; sed -e 's/^/lame /' step2 > step3 ; chmod +x step3 ; ./step3
    bigbrovar · 2009-03-08 17:22:38 5

  • 0
    find . -type d -exec chmod XXXX {} \;
    haithamg · 2010-08-24 16:17:26 5
  • a simple command in order to make iptables rules permanent, run @ sudo!


    0
    iptables-save > firewall.conf; rm -f /etc/network/if-up.d/iptables; echo '#!/bin/sh' > /etc/network/if-up.d/iptables; echo "iptables-restore < firewall.conf" >> /etc/network/if-up.d/iptables; chmod +x /etc/network/if-up.d/iptables
    dddddsadasdasd · 2010-11-13 23:58:28 4
  • per comments


    0
    find ./ -type f -exec chmod 644 {} +
    djbender · 2011-01-29 19:13:58 4
  • Should run with sudo


    0
    cd /etc/network/if-up.d && iptables-save > firewall.conf && echo -e '#!/bin/sh -e\niptables-restore < $(dirname $0)/firewall.conf' > iptables && chmod a+x iptables
    pkufranky · 2011-05-01 03:23:26 3
  • Uses find to find and chmod directories recursively.


    0
    find public_html/ -type d -exec chmod 775 {} \;
    Nichlas · 2011-07-23 19:27:27 5
  • Usage: VBoxBlockBoot [Virtual_Machine] [Block_device] Eg: VBoxBlockBoot WinXP /dev/sdc In another words vm=usb; usb=sdc;sudo umount /dev/$usb* ; sudo chmod 777 /dev/$usb ; VBoxManage storageattach $vm --medium ~/raw-HD-4-VB/$usb.vmdk --type hdd --storagectl "IDE Controller" --device 0 --port 0 ; VBoxManage startvm $vm Where vm --> Name of the virtual machine to start usb --> Block device to use. (/dev/sdc) This can used after setup up a boot loader on to my USB pen drive or HDD (After creating Live USB). Here root privilege is needed but not granted to Virtual Box. Thus we can access all our VM.( If we run VBox as root we can't access our VMs). Root privilege is used to - Unmount the storage device - Chmod to full access (777) Requirements:- 1. Device information file (rawvmdk file) created by the following command. Need to run only once. Not bad to run many. VBoxCreateRawDisk() { VBoxManage internalcommands createrawvmdk -filename ~/.rawHD4VB_`basename "$1"`.vmdk -rawdisk "$1"; } 2. Root privilege to umount & chmod 3. Real storage medium (ie /dev/*) (Non-virtual such as USB HD, pen drive, a partition) 4. A virtual m/c already available (here "usb") vm=usb; usb=sdc;sudo umount /dev/$usb* ; sudo chmod 777 /dev/$usb ; VBoxManage storageattach $vm --medium ~/raw-HD-4-VB/$usb.vmdk --type hdd --storagectl "IDE Controller" --device 0 --port 0 ; VBoxManage startvm $vm VBoxBlockBoot() { sudo umount "$2"*; sudo chmod 777 "$2"; VBoxManage storageattach "$1" --medium ~/.rawHD4VB_`basename "$2"`.vmdk --type hdd --storagectl "IDE Controller" --device 0 --port 0 ; VBoxManage startvm "$1"; } Show Sample Output


    0
    VBoxBlockBoot() { sudo umount "$2"*; sudo chmod 777 "$2"; VBoxManage storageattach "$1" --medium ~/.rawHD4VB_`basename "$2"`.vmdk --type hdd --storagectl "IDE Controller" --device 0 --port 0 ; VBoxManage startvm "$1";}
    totti · 2011-07-29 13:04:19 4
  • If you make a mess (like I did) and you removed all the executable permissions of a directory (or you set executable permissions to everything) this can help. It supports spaces and other special characters in the file paths, but it will work only in bash, GNU find and GNU egrep. You can complement it with these two commands: 1. add executable permission to directories: find . type d -print0 | xargs -0 chmod +x 2. and remove to files: find . type d -print0 | xargs -0 chmod -x Or, in the same loop: while IFS= read -r -u3 -d $'\0' file; do case $(file "$file" | cut -f 2- -d :) in :*executable*|*ELF*|*directory*) chmod +x "$file" ;; *) chmod -x "$file" ;; esac || break done 3< <(find . -print0) Ideas stolen from Greg's wiki: http://mywiki.wooledge.org/BashFAQ/020


    0
    while IFS= read -r -u3 -d $'\0' file; do file "$file" | egrep -q 'executable|ELF' && chmod +x "$file"; done 3< <(find . -type f -print0)
    keymon · 2011-08-18 15:37:23 13
  • Creates the .ssh directory on the remote host with proper permissions, if it doesnt exist. Appends your public key to authorized_keys, and verifies it has proper permissions. (if it didnt exist it may have been created with undesireable permissions). *Korn shell syntax, may or may not work with bash


    0
    ssh <user>@<host> 'mkdir -m 700 ~/.ssh; echo ' $(< ~/.ssh/id_rsa.pub) ' >> ~/.ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys'
    Halki · 2011-10-03 15:59:43 3
  • I simply find binary notation more straightforward to use than octal in this case. Obviously it is overkill if you just 600 or 700 all of your files... Show Sample Output


    0
    function right { bc <<< "obase=8;ibase=2;$1"; }; touch foo; chmod $(right 111111011) foo; ls -l foo
    nerd · 2011-11-16 22:43:31 3
  • this requires the use of a throwaway file. it outputs a shell function. assuming the throwaway file is f.tmp usage: >f.tmp;lso f.tmp > f.tmp; . f.tmp;rm f.tmp;lso -l ... notes: credit epons.org for the idea. however his version did not account for the sticky bit and other special cases. many of the 4096 permutations of file permissions make no practical sense. but chmod will still create them. one can achieve the same sort of octal output with stat(1), if that utility is available. here's another version to account for systems with seq(1) instead of jot(1): lso(){ case $# in 1) { case $(uname) in FreeBSD) jot -w '%04d' 7778 0000 7777 ;; *) seq -w 0000 7777 ;; esac; } \ |sed ' /[89]/d s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \ |sh \ |{ echo "lso(){"; echo "ls \$@ \\"; echo " |sed '"; sed ' s, ,@,2; s,@.*,,; s,\(.* \)\(.*\),s/\2/\1/,; s, ,,'; echo \'; echo }; }; ;; *) echo "usage: lso tmp-file"; ;; esac; } this won't print out types[1]. but its purpose is not to examine types. its focus is on mode and its purpose is to make mode easier to read (assuming one finds octal easier to read). 1. one could of course argue "everything is a file", but not always a "regular" one. e.g., a "directory" is really just a file comprising a list.


    0
    lso(){ jot -w '%04d' 7778 0000 7777 |sed '/[89]/d;s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \ |sh \ |{ echo "lso(){";echo "ls \$@ \\";echo " |sed '";sed 's, ,@,2;s,@.*,,;s,\(.* \)\(.*\),s/\2/\1/,;s, ,,';echo \';echo };};}
    argv · 2012-01-08 05:48:24 4

  • 0
    sudo find foldername -type d -exec chmod 755 {} ";"
    megger · 2012-03-20 21:24:36 4

  • 0
    sudo find foldername -type f -exec chmod 644 {} ";"
    megger · 2012-03-20 21:25:10 3
  • This command finds all files in a folder recursively and sets owner and group to read and write. Leaves all dirs intact. This command does takes care of file names with spaces as well.


    0
    find . -type f -print0 | xargs -0 chmod -v gu=rw
    Tobbera · 2012-03-22 03:08:53 4

  • 0
    find . -type f -exec chmod a-x {} \;
    hpyhacking · 2012-06-11 02:41:47 4
  •  < 1 2 3 4 > 

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands


Check These Out

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Rename files in batch

Download an Entire website with wget

Set laptop display brightness
Run as root. Path may vary depending on laptop model and video card (this was tested on an Acer laptop with ATI HD3200 video). $ cat /proc/acpi/video/VGA/LCD/brightness to discover the possible values for your display.

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"\n" "export AWS_SESSION_TOKEN=\"" $3 "\"" }'` You must adapt the command line to include: * $MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

a short counter
Maybe you know shorter ?

Log colorizer for OSX (ccze alternative)
Download colorizer by @raszi @ http://github.com/raszi/colorize

add all files not under version control to repository
This should handle whitespaces well and will not get confused if your filenames have "?" in them

Sort netflow packet capture
Sort netflow packet capture by unique connections excluding source port.

back ssh from firewalled hosts
host B (you) redirects a modem port (62220) to his local ssh. host A is a remote machine (the ones that issues the ssh cmd). once connected port 5497 is in listening mode on host B. host B just do a ssh 127.0.0.1 -p 5497 -l user and reaches the remote host'ssh. This can be used also for vnc and so on.


Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: