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

Query Wikipedia via console over DNS
Query Wikipedia by issuing a DNS query for a TXT record. The TXT record will also include a short URL to the complete corresponding Wikipedia entry.You can also write a little shell script like: $ $ cat wikisole.sh $ #!/bin/sh $ dig +short txt ${1}.wp.dg.cx and run it like $ ./wikisole.sh unix were your first option ($1) will be used as search term.

grab all commandlinefu shell functions into a single file, suitable for sourcing.
Each shell function has its own summary line, as a comment. If there are multiple shell functions with the same name, the function with the highest number of votes is put into the file. Note: added 'grep -v' to the end of the pipeline, to eliminate extraneous lines containing only '--'. Thanks to matthewbauer for pointing this out.

Send a file to a pastebin from STDIN or a file, with a single function
echo "Hello world!" | sprunge # Redirect a stream to a pastebin sprunge ~/.bashrc # Send a file to a pastebin

Update your OpenDNS network ip
Intended for dynamic ip OpenDNS users, this command will update your OpenDNS network IP. For getting your IP, you can use one of the many one-liners here on commandlinefu. Example: I use this in a script which is run by kppp after it has successfully connected to my ISP: --- #!/bin/bash IP="`curl -s http://checkip.dyndns.org/ | grep -o '[[:digit:].]\+'`" PW="hex-obfuscated-pw-here" if [ "$IP" == "" ] ; then echo 'Not online.' ; exit 1 else wget -q --user=topsecret --password="`echo $PW | xxd -ps -r`" 'https://updates.opendns.com/nic/update?hostname=myhostname&myip='"$IP" -O - /etc/init.d/ntp-client restart & fi --- PS: DynDNS should use a similar method, if you know the URL, please post a comment. (Something with members.dyndns.org, if I recall correctly)

show framebuffer console modes to use in grub vga option
look at /boot/grub/menu.lst for somethig like: ## additional options to use with the default boot option, but not with the ## alternatives ## e.g. defoptions=vga=791 resume=/dev/hda5 ## defoptions=vga=795 # defoptions=vga=873 ## altoption boot targets option ## multiple altoptions lines are allowed ## e.g. altoptions=(extra menu suffix) extra boot options ## altoptions=(recovery) single # altoptions=(verbose mode) vga=775 debug # altoptions=(console mode) vga=ask # altoptions=(graphic mode) quiet splash # altoptions=(recovery mode) single vga=(decimal value) is framebuffer mode

find unreadable file

Log the current memory statistics frequently to syslog
Uses logger in a while loop to log memory statistics frequently into the local syslog server.

Convert control codes to visible Unicode Control Pictures
Converts control codes and spaces (ASCII code ≤ 32) to visible Unicode Control Pictures, U+2400 ? U+2420. Skips \n characters, which is probably a good thing.

Alert on high ping to know if it's really laggy while playing
Online games have pretty good lag compensation nowadays, Sometimes though, you really want to get some warning about your latency, e.g. while playing Diablo III in Hardcore mode, so you know when to carefully quit the game b/c your flatmate started downloading all his torrents at once. This is done on Darwin. On Linux/*nix you would need to find another suitable command instead of `say` to spell out your latency. And I used fping because it's a little bit easier to get the latency value needed. Something similar with our regular ping command could look like this: $ while :; do a=$(ping -c1 google.com | grep -o 'time.*' | cut -d\= -f2 | cut -d\ -f1 | cut -b1-4); [[ $a > 40 ]] && say "ping is $a"; sleep 3; done

A DESTRUCTIVE command to render a drive unbootable
THIS COMMAND IS DESTRUCTIVE. That said, lets assume you want to render your boot drive unbootable and reboot your machine. Maybe you want it to boot off the network and kickstart from a boot server for a fresh OS install. Replace /dev/fd0 with the device name of your boot drive and this DESTRUCTIVE command will render your drive unbootable. Your BIOS boot priority should be set to boot from HD first, then LAN.


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: