You are not logged in.

#1 2010-11-17 05:38:46

greenmanwitch
Member
Registered: 2009-04-20
Posts: 21

Using bash as your file manager?

Hello,

My belief is that all file managers suck. There are no exceptions to this. So, for the past few months, I've been sourcing a file with a bunch of tricks I've invented / found through browsing the web to make using just bash as a file manager much more convenient.

Here's what I currently use:

# fm v1.9.1 by Kiah Morante
# A very simple file manager.
# Depends on pycp/pymv, http://github.com/yannicklm/pycp and feh
# 'source' this file in a BASH shell

showHidden=0 # Hidden files not shown
showDetails=0 # ls is replaced with ls -lh if showDetails is 1

shopt -s autocd # cd to a dir just by typing its name
PROMPT_COMMAND='[[ ${__new_wd:=$PWD} != $PWD ]] && list; __new_wd=$PWD' # ls after cding

# Shortcuts
source ~/.config/fm/shortcuts # Call all custom shortcuts

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias h='cd ~'
alias n='cd "$n"'

# Keybindings

bind '"\C-l":"list\C-m"'
bind '"\C-h":"hide\C-m"'
bind '"\C-o":"details\C-m"'
bind '"\C-f":"makedir\C-m"'
bind '"\C-n":"n\C-m"'
bind '"\C-y":"cpwd\C-m"'
bind '"\C-p":"cd "$OLDPWD"\C-m"' # Hint: You could also type '~-'

# FM prompt appearance

if [[ $(whoami) == 'root' ]]; then
    # So that the user knows if they have root privileges:
    PS1="\[\e[0;32\]mf\[m\e[m\] \[\e[0;31m\]root\[\e[m\] \[\e[0;34m\]\w \[\e[m\]\[\e[0;31m\]> \[\e[m\]"
else
    PS1="\[\e[0;32\]mf\[m\e[m\] \[\e[0;34m\]\w \[\e[m\]\[\e[0;31m\]> \[\e[m\]"
fi

# Functions

# Usage
fmhelp () {
    echo "hide - toggle hidden (hidden by default)
ls - lists contents of dir(s) passed in args.
lsd - list directories
cd - changed to directory \$1
cp \$@ \$2 - copies file from \$1 to \$2
mv \$@ \$2 - moves file from \$1 to \$2
rm \$@ - deletes \$@
sc \$1 \$2 - make a shortcut called \$1 pointing to \$2. If no \$2 is passed, it is evaluated as \$PWD
cpwd - copy current working directory
.., ..., .... - cd .. etc.
o \$1 - opens \$1 with xdg-open
hm - how many files are in the current directory
details - show file details (ls -lh)
fmhelp - this help menu
n - Intelligent guess of the next dir you wish to cd to. Last $1 in open, list, or makedir; last argument in copy or move; pwd before a cd
~- - BASH shortcut for \$OLDPWD

img - feh frontend with the following usage:
      img -t \$2 - views the dirs/images specified in \$2..\$n as clickable thumbnails
      img -s \$2 \$3 - views the images specified in \$3..\$n as a slideshow with a slide change speed of \$2 seconds
      img \$@ - views the dirs/images specified 

Shortkeys:

Ctrl-f - mkdir
Ctrl-h - hide
Ctrl-l - ls
Ctrl-n - cd \$n
Ctrl-o - details
Ctrl-p - cd \$OLDPWD
Ctrl-y - cpwd
Ctrl-u - clear line # urxvt default"
}

# Toggle display hidden files
# If $showHidden is 1, hidden files are shown
hide () { 
    showHidden=$(( 1 - $showHidden ))
    list
}

# Toggle display file details
# If $showDetails is 1, file details are shown
details () { 
    showDetails=$(( 1 - $showDetails ))
    list
}


# ls
listToggle () {
    if [[ $showHidden == 1 && $showDetails == 1 ]]; then
        ls -C --color -A -lh "$dir"
    elif [[ $showHidden == 1 && $showDetails == 0 ]]; then
        ls -C --color -A "$dir"
    elif [[ $showHidden == 0 && $showDetails == 1 ]]; then
        ls -C --color -lh "$dir"
    else
        ls -C --color "$dir"
    fi
}

list () {
    clear # Unclutter the screen
    
    # List pwd if no $1
    if [[ $@ == "" ]]; then
        set '.'
    fi

    # List multiple folders:
    for dir in "$@"
    do
        listToggle
    done

    n="$1" # See 'n' in fmhelp
}

# use feh to view thumbnails/images/slideshow

img () {

    case "$1" in
       -t)       nohup feh --thumbnails "${@:2}" --thumb-height 120 --thumb-width 120 -S filename -d --cache-thumbnails -B black > /dev/null 2>&1 & ;;
       -s)       nohup feh "${@:3}" -S filename -d -B black --slideshow-delay "$2" > /dev/null 2>&1 & ;;
       *)        nohup feh "$@" -S filename -d -B black > /dev/null 2>&1 & ;; 
    esac

    list
}

# cp
copy () {
    if [[ $showHidden == 1 ]]; then
        pycp --interactive --all "$@"
    else
        pycp --interactive "$@"
    fi

    list
    n="${@:(-1)}" # n is the last argument (where stuff is moved to)
}

# mv
move () {
    if [[ $showHidden == 1 ]]; then
        pymv --interactive --all "$@"
    else
        pymv --interactive "$@"
    fi

    list
    n="${@:(-1)}"
}

makedir () {
     if [[ $1 == "" ]]; then
         read -e n
         set "$n"
     fi

     if mkdir -- "$1"; then
         list # Update pwd to show new dir(s) that have been made.
         n="$1" 
     fi
}

# rm
remove () {
     rm -rfI "$@"
     list
}

# open files
o () {
    # To use xdg-open
    #nohup xdg-open "$1" > /dev/null 2>&1 &

    if [ -f "$1" ] ; then
       case "$1" in
       *.tar.bz2)   tar xjf "$1"    ;;
       *.tar.gz)    tar xzf "$1"    ;;
       *.bz2)       bunzip2 "$1"    ;;
       *.rar)       rar x "$1"      ;;
       *.gz)        gunzip "$1"     ;;
       *.tar)       tar xf "$1"     ;;
       *.tbz2)      tar xjf "$1"    ;;
       *.tgz)       tar xzf "$1"    ;;
       *.zip)       unzip "$1"      ;;
       *.Z)         uncompress "$1" ;;
       *.7z)        7z x "$1"       ;;
       *.pdf)        nohup zathura "$1" > /dev/null 2>&1 &     ;;
       *.html)      nohup luakit "$1" > /dev/null 2>&1 &      ;;
       *.blend)     nohup blender "$1" > /dev/null 2>&1 &     ;;
       *.avi)       nohup mplayer "$1"  ;;
       *.wmv)       nohup mplayer "$1"  ;;
       *.rmvb)      nohup mplayer "$1"  ;;
       *.mp3)       nohup urxvtc -si -sw -sh 30 -e mplayer "$1" > /dev/null 2>&1 &     ;;
       *.flv)       nohup mplayer "$1"  ;;
       *.mp4)       nohup mplayer "$1"  ;;
       *.ogg)       nohup urxvt -si -sw -sh 30 -e mplayer "$1" > /dev/null 2>&1 &      ;;
       *.wav)       nohup audacity "$1" > /dev/null 2>&1 &    ;;
       *.jpg)       img "$1"        ;;
       *.jpeg)      img "$1"        ;;
       *.JPG)       img "$1"        ;;
       *.png)       img "$1"        ;;
       *.gif)       nohup gpicview "$1" > /dev/null 2>&1 &    ;;
       *)           nohup urxvt -si -sw -sh 30 -e vim "$1" > /dev/null 2>&1 &          ;;
       esac
    else
        echo "'$1' is not a valid file"
    fi

    n="$1"
}

# Add shortcuts

makeShortcut () {
    if [[ $2 == "" ]]; then
        set $1 .
    fi

    echo ""$1"=\""$2"\"
    alias "$1"='cd \""$2"\"'
         " >> ~/.config/fm/shortcuts

    source ~/.config/fm/shortcuts
}

# Copy pwd to clipboard

cpwd () {
    echo \"$(pwd)\" | xclip
}

# List directories

lsd () {
    ls -F "$@" | grep \/$
}

# Command aliases

alias mv="move"
alias sc="makeShortcut"
alias cp="copy"
alias ls="list"
alias rm="remove"
alias mkdir="makedir"
alias hm="ls -l . | egrep -c '^-'"

list # ls when fm starts

Could all of you fellow file manager-haters post your little tricks, whether just a few lines added to ~/.bashrc or fully fledged files that you source like mine?

Last edited by greenmanwitch (2011-02-07 19:58:40)

Offline

#2 2010-11-17 05:44:36

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Using bash as your file manager?

I haven't created any special commands/bash scripts although I do have a few aliases to make life easier.

I just use the default commands for most everything.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#3 2010-11-17 05:50:38

greenmanwitch
Member
Registered: 2009-04-20
Posts: 21

Re: Using bash as your file manager?

shopt -s autocd # cd to a dir just by typing its name
PROMPT_COMMAND='[[ ${__new_wd:=$PWD} != $PWD ]] && list; __new_wd=$PWD'

This one is definitely my favourite.

EDIT: Someone made me realise that my old way of doing this was pretty broken and dodgy. This is a much better fix.

Last edited by greenmanwitch (2010-11-17 12:23:23)

Offline

#4 2010-11-17 06:08:34

frabjous
Member
Registered: 2010-07-13
Posts: 367

Re: Using bash as your file manager?

greenmanwitch wrote:
shopt -s autocd # cd to a dir just by typing its name
PROMPT_COMMAND='if [[ $PWD != $OLDPWD ]]; then list; fi' # ls after cd to a new dir

This one is definitely my favourite.

That is pretty nice. I don't suppose there's a way to make it work with any subfolder of any directory in CDPATH?

Up till now, I've just been using a function to have cd do its normal thing and then call ls -F:

function cd {
    builtin cd "$@" && ls -F
}

Offline

#5 2010-11-17 10:46:54

ijanos
Member
From: Budapest, Hungary
Registered: 2008-03-30
Posts: 443

Re: Using bash as your file manager?

Check out viewglob, it is an interesting idea to extend command line filemanagement.

you can find a pkgbuild in AUR.

Offline

#6 2010-11-17 12:21:22

aeosynth
Member
From: California
Registered: 2010-02-06
Posts: 115
Website

Re: Using bash as your file manager?

`cd -` is the same as `cd "$OLDPWD"`.

I extract my zip and rar files to a new directory:

x() {
  case $1 in
  *.rar)
      dir="${1%.rar}"
      mkdir "$dir"
      unrar e "$1" "$dir" >/dev/null
      ;;
  *.zip)
      dir="${1%.zip}"
      mkdir "$dir"
      unzip "$1" -d "$dir"
      ;;
  *)     bsdtar -xf $1;;
  esac
}

Offline

#7 2010-11-17 16:20:08

GogglesGuy
Member
From: Rocket City
Registered: 2005-03-29
Posts: 610
Website

Re: Using bash as your file manager?

greenmanwitch wrote:

Hello,

My belief is that all file managers suck. There are no exceptions to this. So, for the past few months, I've been sourcing a file with a bunch of tricks I've invented / found through browsing the web to make using just bash as a file manager much more convenient.

If all file managers suck, then by definition this one sucks as well... big_smile

Offline

#8 2010-11-17 16:23:24

ijanos
Member
From: Budapest, Hungary
Registered: 2008-03-30
Posts: 443

Re: Using bash as your file manager?

GogglesGuy wrote:

If all file managers suck, then by definition this one sucks as well... big_smile

Eh smile Dont be a troll. Bash is a shell, which can be used as a file manager. He stated that the programs that call themselves file-managers suck.

Offline

#9 2010-11-17 16:45:20

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Using bash as your file manager?

I long ago got into the habit of cd.

cat .config/fish/fish_history | grep -v '^#' | awk '{print $1}' | sort | uniq -c | sort -rn | head -3
  15203 ls
   9949 cd
   8629 exit

aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#10 2010-11-17 16:52:46

ijanos
Member
From: Budapest, Hungary
Registered: 2008-03-30
Posts: 443

Re: Using bash as your file manager?

fsckd wrote:

I long ago got into the habit of cd.

cat .config/fish/fish_history | grep -v '^#' | awk '{print $1}' | sort | uniq -c | sort -rn | head -3
  15203 ls
   9949 cd
   8629 exit

exit? Really? You know you can just type ctrl-d, right?

Offline

#11 2010-11-17 16:57:05

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Using bash as your file manager?

I got into the habit of typing exit. sad


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#12 2010-11-17 22:05:48

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: Using bash as your file manager?

I never saw the point in using a 'file manager'.  Using the command line for that kind of stuff is half the fun of using Linux imo.  Plus I've found that by using command line file management, my directory tree ends up being far better organized than it ever was using graphical file management.  It doesn't take long to learn all the tricks and make cli file management much faster and easier than with a graphical file manager. 

I mostly just use the default commands for everything, and then have a small library of scripts for most things.  the best tip I can suggest is try 'shopt -s extglob'

here's a little tidbit about extglob and how cool it is http://aplawrence.com/Words2005/2005_05_25.html


Hofstadter's Law:
           It always takes longer than you expect, even when you take into account Hofstadter's Law.

Offline

#13 2010-11-17 22:58:37

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: Using bash as your file manager?

# Depends on pycp/pymv, http://github.com/yannicklm/pycp

Hey, that's me smile

(pycp-git is in AUR too)


Just a word of warning: pycp may break quite often.

Don't hesitate to provide feedback here:
https://bbs.archlinux.org/viewtopic.php?id=65331

Offline

#14 2010-11-18 04:28:07

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Using bash as your file manager?

I use all of these extra bash features

# bash options
shopt -s cdspell                    # autocorrects cd misspellings
shopt -s checkwinsize               # update the value of LINES and COLUMNS after each command if altered
shopt -s cmdhist                    # save multi-line commands in history as single line
shopt -s dotglob                    # include dotfiles in pathname expansion
shopt -s expand_aliases             # expand aliases
shopt -s extglob                    # enable extended pattern-matching features
shopt -s nocaseglob                 # pathname expansion will be treated as case-insensitive
shopt -s histappend                 # Append History instead of overwriting file.
shopt -s no_empty_cmd_completion    # No empty completion

Some more than the others of course. extglob, expand_aliases and dotglob are the most used for me.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#15 2010-11-18 06:12:52

hatten
Arch Linux f@h Team Member
From: Sweden, Borlange
Registered: 2009-02-23
Posts: 736

Re: Using bash as your file manager?

Aliasing h to cd ~ may be a little overkill, as just 'cd' does the same as 'cd ~', so you're just saving one character.

Offline

#16 2010-11-18 08:29:25

archman-cro
Member
From: Croatia
Registered: 2010-04-04
Posts: 943
Website

Re: Using bash as your file manager?

hatten wrote:

Aliasing h to cd ~ may be a little overkill, as just 'cd' does the same as 'cd ~', so you're just saving one character.

Hah! Never knew about using just "cd" to get in the home dir! Nice tip!

Offline

#17 2010-11-18 10:57:27

Zom
Member
From: Sweden
Registered: 2007-10-27
Posts: 430

Re: Using bash as your file manager?

archman-cro wrote:
hatten wrote:

Aliasing h to cd ~ may be a little overkill, as just 'cd' does the same as 'cd ~', so you're just saving one character.

Hah! Never knew about using just "cd" to get in the home dir! Nice tip!

Maybe a bit OT, but you can also use 'cd -' to go into the previous directory you were in. smile

$ cd /var/tmp
$ cd /media/cd
$ cd -
(And you're back in /var/tmp)

Offline

#18 2010-11-18 13:06:17

poltak
Member
From: Hong Kong
Registered: 2010-11-05
Posts: 32

Re: Using bash as your file manager?

Just out of curiosity, what don't you guys like about file managers?


I like Arch

Offline

#19 2010-11-18 14:06:46

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: Using bash as your file manager?

poltak wrote:

Just out of curiosity, what don't you guys like about file managers?

For me, it's mostly that I can type faster than I can click and drag.  I find moving files around in a file manager tedious and slow.
once you are used to using the command line for everything you realize that it's actually quite a bit faster than pushing your mouse around for everything.  especially once you get into scripting common tasks.


Hofstadter's Law:
           It always takes longer than you expect, even when you take into account Hofstadter's Law.

Offline

#20 2010-11-18 15:03:58

archman-cro
Member
From: Croatia
Registered: 2010-04-04
Posts: 943
Website

Re: Using bash as your file manager?

Try Ranger. Cli file manager. smile

Offline

#21 2010-11-19 01:44:50

3])
Member
From: Netherlands
Registered: 2009-10-12
Posts: 215

Re: Using bash as your file manager?

I would actually state that using bash as your filemanager is good for a 'few' files, but once you have video files cluttered all throughout your hard drive and folders all over, thats where the 'bash' filemanager system lacks its use in terms of effectiveness.


I must state that however I say that, I use a bash terminal to sometimes go through folders myself and it does feel a bit natural. Just being the devils advocate.


some bits from my .bashrc

alias ll="ls -lh"
alias la="ls -a"
alias exit="clear; exit"

alias ls="ls --color=auto"

I like the use of aliases.


“There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”-- C.A.R. Hoare

Offline

#22 2010-11-19 02:51:18

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Using bash as your file manager?

3]) wrote:

I would actually state that using bash as your filemanager is good for a 'few' files, but once you have video files cluttered all throughout your hard drive and folders all over, thats where the 'bash' filemanager system lacks its use in terms of effectiveness.

You are correct for multimedia files but for text (even rich text) the command line tools are unparalleled; recall find and grep. The shell one chooses also matters. Bash is great but zsh has superior globbing features. Personally I use fish which alows me to jump to directories much faster than any file manager, even Ranger.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#23 2010-11-19 04:23:54

frabjous
Member
Registered: 2010-07-13
Posts: 367

Re: Using bash as your file manager?

3]) wrote:

I would actually state that using bash as your filemanager is good for a 'few' files, but once you have video files cluttered all throughout your hard drive and folders all over, thats where the 'bash' filemanager system lacks its use in terms of effectiveness.

Wow, precisely what moved me away from using graphical filemanagers is how unresponsive they became when there were a large number of multimedia files in a given folder (--every try to open a folder in nautilus with 1000+ images?), and how long they take to search the entire filesystem for certain kinds of files when they were spread around (as compared to locate/find in a terminal).

Offline

#24 2010-11-19 13:46:08

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: Using bash as your file manager?

3]) wrote:

once you have video files cluttered all throughout your hard drive and folders all over, thats where the 'bash' filemanager system lacks its use in terms of effectiveness.

Actually, I found this to be one of the best advantages of using bash is that it forces a user to think about file organization and making useful naming schemes for files.
For example, instead of having 1000+ media files in one directory I subcategorize theme by genre or whatever, and then probably subcategorize them again.
Then I usually rename the files to something meaningful, like if I have 50 pictures of my kids birthday, just do a for each loop on the directory and rename all the files donovan_birthdayX.jpg where X is an integer incrementation.

essentially. just don't "have files cluttered all throughout you hard drive and folders all over". and your life will be much happier regardless of how you manage your files.


Hofstadter's Law:
           It always takes longer than you expect, even when you take into account Hofstadter's Law.

Offline

#25 2010-11-19 16:04:54

Awebb
Member
Registered: 2010-05-06
Posts: 6,275

Re: Using bash as your file manager?

3]) wrote:

I would actually state that using bash as your filemanager is good for a 'few' files, but once you have video files cluttered all throughout your hard drive and folders all over, thats where the 'bash' filemanager system lacks its use in terms of effectiveness.

Indeed. I love features like Ctrl+click to highlight files that have nothing in common and symlink them to another folder by drag'n'drop('n hold a key).

I usually use Nautilus when I don't really know what I want to do with which file. I also use it with file-roller. I seem to be unable to memorize the tar options. tar -xfce :-D

Offline

Board footer

Powered by FluxBB