Commands using basename (18)

  • 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 313

  • 12
    find /proc -user myuser -maxdepth 1 -type d -mtime +7 -exec basename {} \; | xargs kill -9
    sharfah · 2009-10-05 14:49:51 10
  • 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 5
  • 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 50

  • 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 13
  • 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 1168
  • _Exit __clone2 _exit _llseek _newselect _syscall _sysctl accept accept4 access acct add_key ...


    1
    find $(man --path | tr ':' ' ') -type f -path '*man2*' -exec basename {} \; | sed 's/\..*//' | sort
    wuseman1 · 2024-08-26 18:29:40 93
  • 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 6

  • 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 7
  • 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 8
  • 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 10
  • 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 9
  • 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

List all Samba user name

Retrieve the size of a file on a server
Downloads the entire file, but http servers don't always provide the optional 'Content-Length:' header, and ftp/gopher/dict/etc servers don't provide a filesize header at all.

find and delete empty dirs, start in current working dir
A quick way to find and delete empty dirs, it starts in the current working directory. If you do find . -empty -type d you will see what could be removed, or to a test run.

Find all active ip's in a subnet

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Format date/time string for a different day
The "date' command has options to easily format the date, day, month, time, etc. But what if you want a relative date or time. Like, I wanted yesterday's date in a particular format. You may want the exact date of "2 months ago" or "-3 days" nicely formatted. For that, you can use this command. The --date option takes fuzzy parameters like the ones mentioned in the previous sentence.

List only executables installed by a debian package
Safe for whitespaces in names.

switch case of a text file

Write comments to your history.
A null operation with the name 'comment', allowing comments to be written to HISTFILE. Prepending '#' to a command will *not* write the command to the history file, although it will be available for the current session, thus '#' is not useful for keeping track of comments past the current session.

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"


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: