You are not logged in.

#26 2010-11-19 22:50:15

rwd
Member
Registered: 2009-02-08
Posts: 664

Re: Using bash as your file manager?

I guess if you move around files all day it pays off to learn every shortcut and commandline trick (A great script for  navigating through folders quickly is autojump b.t.w). For just a bit of casual file management Thunar works fine i.m.o.. It is also quite flexible with it's custom actions and batch rename function.

Offline

#27 2010-11-20 05:45:59

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Using bash as your file manager?

archman-cro wrote:

Try Ranger. Cli file manager. smile

I have ranger installed for when I'm looking for something and I don't know what it is; Drifting -- Searching for clues. Otherwise, bash is where I do most of my file work. Other shells are interesting, but I've haven't found a need that's driven me to one of them.

Offline

#28 2010-11-22 14:11:17

csn
Member
Registered: 2009-02-28
Posts: 10

Re: Using bash as your file manager?

I stumbled across this little script gem some years ago and have been using it very heavily ever since.

It's based on the idea that filenames are ultimately text and editing them would best be done with your text editor of choice.

I modified it slightly so that it also creates directories when needed (my version is appended below). This isn't extensively tested however. I found I seldomly use it for renaming whole hierarchies after all. I was also planning on adding support for copying (not just renaming) at some point, as well as using absolute pathnames for everything. Just didn't get around to doing it yet.

Apart from that I only use coreutils and zsh's `EXTENDED_GLOB' to make life easier.

#!/bin/sh
# emv - editor mv
#
# Description:
# Rename files with your favourite text editor.
# or: Edit file names using the "document metaphor" ;-) 
#
# This script is intended to be Posix-compliant.
# Tested with coreutils 5.2.1, dash 0.5.2, bash 3.0 and sed 4.1.4.
#
# It was initiated during another nice LUG Sinsheim meeting
# by Torsten Scheck, Uwe Menges, Volker Weiss.
# Special thanks to Marc Lehmann for many valuable hints
# which made emv actually usable.
#
# v1.95 2006-02-17 torsten.scheck@gmx.de
# Latest Version: http://www.i0i0.de/toolchest/emv
#
# Knows issues:
# * Names must not contain new line characters (0x0a)
# * A directory and its entries can't be renamed in the same 
#   emv session.
# * before bash 3.1 the builtin read removes 0x7f, so file names 
#   containing that character can't be renamed.
# * dash 0.5.2 removes 0x81 and 0x88 from variables, so 
#   file names containing those characters won't be listed.
# * dash 0.5.2's builtin read combines '\\' to '\'
#
# This software is provided by the contributors "as is" and any
# express or implied warranties, including, but not limited to, the
# implied warranties of merchantability and fitness for a
# particular purpose are disclaimed. In no event shall the
# contributors be liable for any direct, indirect, incidental,
# special, exemplary, or consequential damages (including, but not
# limited to, procurement of substitute goods or services; loss of
# use, data, or profits; or business interruption) however caused
# and on any theory of liability, whether in contract, strict
# liability, or tort (including negligence or otherwise) arising in
# any way out of the use of this software, even if advised of the
# possibility of such damage.
#
# Copyright is hereby assigned to the Public Domain.

edit=${VISUAL:-${EDITOR:-vi}}
if [ $# -eq 0 ]; then
    echo "Usage: $0 FILE...
    Rename FILEs with your favourite text editor ($edit), 
    specified by the VISUAL or EDITOR environment variable." 
    exit 0
fi

# create private temp dir and ensure its deletion
umask 077
dir="/var/tmp/emv-$(id -u)-$$"
[ ! -d "$dir" ] && mkdir "$dir"
cleanexit () { rm -rf "$dir"; exit $1; }
trap 'cleanexit 0' 2 15

orgfile="$dir/org-$$"
newfile="$dir/new-$$"
diffile="$dir/dif-$$"
tmpfile="$dir/tmp-$$"
errfile="$dir/err-$$"

# list file names into temp files
# remove trailing '/' of directories passed to allow intermediate renaming step
ls -1d -- "$@" | sed 's/\/$//' | tee "$orgfile" >"$newfile"

# files not found
[ ! -s "$orgfile" ] && cleanexit 1
# detect file names with newlines
if [ $(ls -q -1d -- "$@" | wc -l) -ne $(wc -l <"$orgfile") ]; then
    echo "This script doesn't work for file names containing newline characters. Exiting." >&2
    cleanexit 1
fi

while
    if [ -t 0 ]; then
        "$edit" "$newfile"
    else
        # workaround: for find/xargs: <&1 prevents that editor gets confused by /dev/null being attached 
        # to stdin; not all editors are able to find an appropriate terminal, though.
        "$edit" "$newfile" <&1
    fi
    # remove empty lines
    sed '/^$/ d' "$newfile" > "$tmpfile"
    mv "$tmpfile" "$newfile"
    # detect line count mismatch
    diff=$(( $(wc -l <"$orgfile") - $(wc -l <"$newfile") ))
    [ $diff -ne 0 ]
do
    echo "Line count doesn't match by $diff lines. (press 'Enter' to continue, Ctrl-C to exit)" 
    read enter
done
# quit if file wasn't changed (only if 'cmp' is available)
type cmp >/dev/null 2>&1 && cmp "$orgfile" "$newfile" >/dev/null 2>&1 && cleanexit 0

# remove IFS default value to prevent 'read' from trimming space and tabs
IFS=''
# copy old and new content of changed lines into temp file
exec 3< "$newfile"
while read -r orgline; do 
    read -r newline <&3
    # workaround: dash doesn't like '!' or '('
    if [ "x${orgline}" != "x${newline}" ]; then
        tmpline="$orgline"
        # intermediate renaming step to prevent collision of file names (a->b; b->a) 
        # only if target is not a directory
        if [ ! -d "$newline" ]; then
            tmpline="${orgline}.tmp$$~"
            # -i: safety net, but should have no effect; stdin in is not connected to a terminal
            mv -i -- "$orgline" "$tmpline" 2>>"$errfile" 
            # mark error in intermediate renaming step 
            [ "$?" -ne "0" ] && { newline="err"; orgline="err"; }
        fi
        echo --"$tmpline" >>"$diffile"
        echo --"$newline" >>"$diffile"
        echo --"$orgline" >>"$diffile"
    fi
done <"$orgfile"

# verbosely rename files 
if [ -e "$diffile" ]; then 
    # workaround: echo does not interpret '--' to mean the end of options
    cut -c 3- "$diffile" > "$tmpfile"
    mv "$tmpfile" "$diffile"
    while read -r tmpline && read -r newline && read -r orgline; do 
        # skip errors in intermediate renaming step 
        if [ "x${orgline}" != "x${newline}" ]; then
            # shell doesn't like '<' '=' '>' at this point; workaround?
            if [ -e "$newline" -a ! -d "$newline" ]; then 
                # don't overwrite existing files
                echo "File '$newline' already exists - '$orgline' remains unchanged." >>"$errfile"
                false
            else
                mkdir -v -p $(dirname "$newline") 2>>"$errfile"
                echo " '""$orgline""'" "->" "'""$newline""'"
                mv -i -- "$tmpline" "$newline" 2>>"$errfile" 
            fi
            # if renaming failed, revoke intermediate renaming step
            if [ "$?" -ne "0" -a "x${tmpline}" != "x${orgline}" ]; then 
                mv -i -- "$tmpline" "$orgline" 2>>"$errfile"
            fi
        fi
    done <"$diffile" 
fi

# list errors
[ -e "$diffile" ] && { [ -s "$errfile" ] && cat "$errfile" || echo "All files have been successfully renamed/moved."; }
cleanexit 0

Offline

#29 2010-12-01 01:18:09

Sara
Member
From: USA
Registered: 2009-07-09
Posts: 219
Website

Re: Using bash as your file manager?

csn wrote:

I stumbled across this little script gem some years ago and have been using it very heavily ever since.

It's based on the idea that filenames are ultimately text and editing them would best be done with your text editor of choice.

I modified it slightly so that it also creates directories when needed (my version is appended below). This isn't extensively tested however. I found I seldomly use it for renaming whole hierarchies after all. I was also planning on adding support for copying (not just renaming) at some point, as well as using absolute pathnames for everything. Just didn't get around to doing it yet.

This script is absolutely life-changing for me. Brings the power of vim now to everything I use (I'm even using vim to type this, via w3m smile )... I can already see a gazallion minutes saved because of this. Thanks a lot.


Registed Linux User 483618

Offline

#30 2010-12-01 10:05:21

Tes--
Member
Registered: 2009-11-13
Posts: 42

Re: Using bash as your file manager?

csn wrote:

I stumbled across this little script gem some years ago and have been using it very heavily ever since.

It's based on the idea that filenames are ultimately text and editing them would best be done with your text editor of choice.

The program vidir in the package community/moreutils does roughly the same. Except, that one you can use to delete files, and you can combine it with find.

Offline

#31 2010-12-01 13:37:33

Sara
Member
From: USA
Registered: 2009-07-09
Posts: 219
Website

Re: Using bash as your file manager?

Tes-- wrote:

The program vidir in the package community/moreutils does roughly the same. Except, that one you can use to delete files, and you can combine it with find.

Thanks for the reply. Hadn't known about the moreutils package, which has a slew of other nice small, useful scripts.


Registed Linux User 483618

Offline

#32 2010-12-01 17:22:13

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Using bash as your file manager?

I like Ranger... it's commandline and fast. What more could you want?

Offline

Board footer

Powered by FluxBB