Posted: . At: 10:45 AM. This was 6 years ago. Post ID: 12248
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Interesting one-liner to batch rename files.


This is a very interesting one-liner. This will batch rename files according to wildcards.

jason@Yog-Sothoth » Pictures » $ for i in *.png; do mv -vi -- "${i}" "$(date +%s%3N).${i#*.}"; done

This renames files with a filename generated using the UNIX epoch.

I guess this could be useful to someone out there.

Simple but useful function to check for bad symlinks in a directory.

function badlink()
# From Atomic magazine #43 August 2004. http://www.atomicmpc.com.au
{
	DEFAULT=$(tput sgr0);
	FILELIST=.badlink.list
 
	[ -e $FILELIST ] && $( rm -fr $FILELIST )
 
	function checklink()
	{
		for badlink in $1/*; do
			[ -h "$badlink" -a ! -e "$badlink" ] && echo \
			\"$badlink\" >> $FILELIST
			[ -d "$badlink" ] && checklink $badlink
		done
	}
 
	for directory in `pwd`; do
		if [ -d $directory ] ; then
			checklink $directory;
		fi
	done
 
	if [ -e $FILELIST ] ; then
		for line in $(cat $FILELIST); do
			echo $line | xargs -r rm | echo -e "$line \
			-removed"
			echo
		done
		rm -fr $FILELIST
	else
		printf "Bad symlinks not found.\n\n"
	fi
} # End Atomic function.

Move all files in a directory to lowercase. This is very useful for cleaning up a directory with a lot of files.

function lowercase()  # move filenames to lowercase.
{
    for file ; do
        filename=${file##*/}
        case "$filename" in
        */*) dirname==${file%/*} ;;
        *) dirname=.;;
        esac
        nf=$(echo $filename | tr A-Z a-z)
        newname="${dirname}/${nf}"
        if [ "$nf" != "$filename" ]; then
            mv "$file" "$newname"
            echo "lowercase: $file --> $newname"
        else
            echo "lowercase: $file not changed."
        fi
    done
}

Very useful variables to have in your .bashrc.

export TIME_STYLE=+" %d-%m-%y %I:%M %P"
# These two examples from bash-doc.
export PAGER="/usr/bin/less"
export VIEWER="mcedit"
export LESS="-i -e -M -P%t?f%f :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-..."
shopt -s checkwinsize
eval `dircolors -b`
alias dir='ls --format="vertical"'
alias lu='ls -hula'
alias vdir='ls --format="long"'
alias verbosetree='tree -A -s -p -f --dirsfirst'
alias psverbose='ps u a x f g'
alias ll='ls -l | sort -nr'
alias la='ls -A'
alias l='ls -CF'
alias dsize="du -ack | sort -nr | head -n 20"
alias psview="pstree -a -u -G -l -h -p"

Another way to copy a file to another filename.

jason@Yog-Sothoth » phone » $ /bin/dd bs=1 if=sulla.zip of=sellafield.zip
55395887+0 records in
55395887+0 records out
55395887 bytes (55 MB, 53 MiB) copied, 74.3485 s, 745 kB/s

Find out how long a user has been logged in to their machine.

jason@Yog-Sothoth » bdoom » $ finger -lmps $LOGNAME | sed -e "s/On/Logged in/g" | grep "since"
Logged in since Tue Jul 17 08:26 (AEST) on :1 from :1 (messages off)

Creating a Linux filesystem in a file.

Using dd, a file may be created, which can be used as a loopback filesystem.

jason@Yog-Sothoth » Documents » $ dd if=/dev/zero bs=1M count=128 > out.img
128+0 records in
128+0 records out
134217728 bytes (134 MB, 128 MiB) copied, 0.0625145 s, 2.1 GB/s

Then create a Linux filesystem in the file.

jason@Yog-Sothoth » Documents » $ sudo mkfs.ext4 out.img
1) All commands run with root privileges are always dangerous.
2) Never run commands on an environment you are not willing to destroy, or able to restore.
3) Do not become root until you know what you are going to do.
4) Be sure of your command and what is going to be affected by it.
[sudo] password for jason: 
mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks: done                            
Creating filesystem with 131072 1k blocks and 32768 inodes
Filesystem UUID: 28502c6a-aeea-422e-99cb-8d717acd1eeb
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

Now, there is a filesystem, which may be mounted to /mnt and used like any other partition.

jason@Yog-Sothoth » Documents » $ sudo fdisk -l out.img 
Disk out.img: 128 MiB, 134217728 bytes, 262144 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Mount it to a directory.

jason@Yog-Sothoth » Documents » $ sudo mount -o loop out.img /mnt/img/

And it is ready for use.

jason@Yog-Sothoth » img » $ df -Hla /mnt/img
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      126M  1.6M  115M   2% /mnt/img

That is how easy it is to create a loopback filesystem and use it for anything you wish.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.