Commands using basename (17)

  • Used for moving stuff around on a fileserver


    18
    mv $1 $2 && ln -s $2/$(basename $1) $(dirname $1)
    svg · 2009-05-25 08:54:36 140

  • 12
    find /proc -user myuser -maxdepth 1 -type d -mtime +7 -exec basename {} \; | xargs kill -9
    sharfah · 2009-10-05 14:49:51 9
  • Useful if you have a list of images called 1 2 3 4 and so on, you can adapt it to rewrite it as 4 (in this example) 0-padded number. Show Sample Output


    5
    for i in ???.jpg; do mv $i $(printf %04d $(basename $i .jpg) ).jpg ; done
    carlesso · 2010-11-18 23:48:41 3
  • Lists a sample of all installed toilet fonts Show Sample Output


    3
    find /usr/share/figlet -name *.?lf -exec basename {} \; | sed -e "s/\..lf$//" | xargs -I{} toilet -f {} {}
    unixmonkey3987 · 2010-07-13 20:12:54 5

  • 2
    find /var/www/html/ -type f -mtime +30 -exec basename {} \;
    lv4tech · 2009-05-07 21:05:47 6
  • This command changes all filename and directories within a directory tree to unaccented ones. I had to do this to 'sanitize' some samba-exported trees. The reason it works might seem a little difficult to see at first - it first reverses-sort by pathname length, then it renames only the basename of the path. This way it'll always go in the right order to rename everything. Some notes: 1. You'll have to have the 'unaccent' command. On Ubuntu, just aptitude install unaccent. 2. In this case, the encoding of the tree was UTF-8 - but you might be using another one, just adjust the command to your encoding. 3. The program might spit a few harmless errors saying the files are the same - not to fear.


    2
    find /dir | awk '{print length, $0}' | sort -nr | sed 's/^[[:digit:]]* //' | while read dirfile; do outfile="$(echo "$(basename "$dirfile")" | unaccent UTF-8)"; mv "$dirfile" "$(dirname "$dirfile")/$outfile"; done
    Patola · 2009-08-24 21:24:18 7
  • Many times I give the same commands in loop to find informations about a file. I use this as an alias to summarize that informations in a single command. Now with variables! :D Show Sample Output


    2
    fileinfo() { RPMQF=$(rpm -qf $1); RPMQL=$(rpm -ql $RPMQF);echo "man page:";whatis $(basename $1); echo "Services:"; echo -e "$RPMQL\n"|grep -P "\.service";echo "Config files:";rpm -qc $RPMQF;echo "Provided by:" $RPMQF; }
    nnsense · 2015-05-11 16:46:01 13
  • Requires: curl xsel access to the internet(http://transfer.sh) This is an alias utilizing the transfer.sh service to make sharing files easier from the command line. I have modified the alias provided by transfer.sh to use xsel to copy the resulting URL to the clipboard. The full modified alias is as follows since commandlinefu only allows 255 characters: transfer() { if [ $# -eq 0 ]; then echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"; return 1; fi if tty -s; then basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g'); curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" |xsel --clipboard; else curl --progress-bar --upload-file "-" "https://transfer.sh/$1" |xsel --clipboard ; fi; xsel --clipboard; } Show Sample Output


    2
    transfer() { basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g');curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile"|xsel --clipboard;xsel --clipboard ; }
    leftyfb · 2016-03-20 19:38:48 12
  • This shell function displays a list of binaries contained in an installed package; works on Debian based Linux distributions. Show Sample Output


    1
    binaries () { for f in $(dpkg -L "$1" | grep "/bin/"); do basename "$f"; done; }
    lordtoran · 2019-10-05 10:37:51 432
  • omit "> ~/Desktop/MyAppList`date +%s.txt`" if you don't want to print it to a file on your desktop and instead only want to display to console created and tested on: ProductName: Mac OS X ProductVersion: 10.6.3 BuildVersion: 10D573 Show Sample Output


    0
    find ~/Music/iTunes/iTunes\ Media/. -name \*.ipa -exec basename {} \; | cut -d \. -f 1 > ~/Desktop/MyAppList`date +%s.txt`
    IsraelTorres · 2010-06-16 15:14:32 5

  • 0
    ffmpeg -i "concat:$(find . -name "*.mp3" | sort | tr '\n' '|')" -acodec copy ../$(basename $(pwd)).mp3 && mp3val -f ../$(basename $(pwd)).mp3
    unixmonkey56961 · 2013-05-12 20:18:47 6
  • Set variable 'input' to a set of flac files.


    0
    IFS=$(echo -en "\n\b"); input="/my/input/dir/*.flac"; mkdir -p $(dirname $f)/mp3; for f in $input; do ffmpeg -i $f -ab 196k -ac 2 -ar 48000 $(dirname $f)/mp3/$(basename "${f:0:${#f}-4}mp3"); done
    marminthibaut · 2013-08-02 19:45:38 7
  • Strips the audio track from a webm video. Use this in combination with clive or youtube-dl.


    0
    for file in "$@"; do name=$(basename "$file" .webm) echo ffmpeg -i $file -vn -c:a copy $name.ogg ffmpeg -i "$file" -vn -c:a copy "$name.ogg" done
    hoodie · 2013-10-05 14:49:07 9
  • Written on OSX after `brew install unrar coreutils`; presumably works on other unices with minimal modifications. Didn't test rars that actually have paths in them, just "flat" files. Won't include files in the rar starting with a dot.


    0
    function rar2zip { rar="$(grealpath "$1")"; zip="$(grealpath "${2:-$(basename "$rar" .rar).zip}")"; d=$(mktemp -d /tmp/rar2zip.XXXXXX); cd "$d"; unrar x "$rar"; zip -r "$zip" *; cd -; rm -r "$d"; }
    epistemenical · 2014-05-28 07:51:17 8
  • Full command: for f in input/*; do BN=$(basename "$f"); ffmpeg -i "$f" -vn "temp/$BN.flac"; sox "temp/$BN.flac" "temp/$BN-cleaned.flac" noisered profile 0.3; ffmpeg -i "$f" -vcodec copy -an "temp/$BN-na.mp4"; ffmpeg -i "temp/$BN-na.mp4" -i "temp/$BN-cleaned.flac" "output/$BN"; done This was over the 255 character limit and I didn't feel like deliberately obfuscating it. 1. Create 'input', 'output' and 'temp' directories. 2. Place the files that you want to remove the hiss/static/general noise from in the input directory. 3. Generate a noise reduction profile with sox using 'sox an_input_file.mp4 -n trim x y noiseprof profile', where x and y indicates a range in seconds that only the sound you want to eliminate is present in. 4. Run the command.


    0
    for f in input/*; do BN=$(basename "$f"); ffmpeg -i "$f" -vn "temp/$BN.flac"...
    samcamwilliams · 2015-03-01 02:48:19 8

  • 0
    basename /etc/environment
    cbarox · 2016-09-02 19:37:46 16
  • CHANGELOG Version 1.1 removedir () { echo "You are about to delete the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=$(echo "$PWD" | sed 's/ /\\ /g'); foo=$(basename "$blah"); rm -Rf ../$foo/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; } BUG FIX: Folders with spaces Version 1.0 removedir () { echo "You are about to delete the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=`basename $PWD`; rm -Rf ../$blah/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; } BUG FIX: Hidden directories (.dotdirectory) Version 0.9 rmdir () { echo "You are about to delete the current directory $PWD. Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=`basename $PWD`; rm -Rf ../$blah/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; } Removes current directory with recursive and force flags plus basic human check. When prompted type yes 1. [user@host ~]$ ls foo bar 2. [user@host ~]$ cd foo 3. [user@host foo]$ removedir 4. yes 5. rm -Rf foo/ 6. [user@host ~]$ 7. [user@host ~]$ ls bar Show Sample Output


    -2
    removedir () { echo "Deleting the current directory $PWD Are you sure?"; read human; if [[ "$human" = "yes" ]]; then blah=$(echo "$PWD" | sed 's/ /\\ /g'); foo=$(basename "$blah"); rm -Rf ../$foo/ && cd ..; else echo "I'm watching you" | pv -qL 10; fi; }
    oshazard · 2010-01-17 11:34:38 31

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

Using parcellite, indents the content of the clipboard manager
This command takes the content of a Parcellite-managed clipboard manager and add one level of indentation to it. It may be useful to indent a block of code which will enter inside another, already indented one but I use it mostly to indent code I will post in Stack Overflow questions and answers.

Multiple variable assignments from command output in BASH
No command substitution but subshell redirection

Sending a file over icmp with hping
you need to start a listening hping on the reciever: hping3 --listen 10.0.2.254 -I eth0 --sign MSGID1 then you can send your file: hping3 10.0.2.254 --icmp --sign MSGID1 -d 50 -c 1 --file a_file

Generate MD5 of string and output only the hash checksum in a readable format
Generates the md5 hash, without the trailing " -" and with the output "broken" into pairs of hexs.

Extract IPv4 addressess from file

lotto generator

Ease your directory exploration
Usage : tt [OCCURRENCE] tt will display a tree from your actual path tt .svn will display only line containing .svn

move contents of the current directory to the parent directory, then remove current directory.
I think this is less resource consuming than the previous examples

Both view and pipe the file without saving to disk
This is a cool trick to view the contents of the file on /dev/pts/0 (or whatever terminal you're using), and also send the contents of that file to another program by way of an unnamed pipe. All the while, you've not bothered saving any extra data to disk, like you might be tempted to do with sed or grep to filter output.

Produce 10 copies of the same string


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: