Useful Linux/UNIX commands.

Posted: December 20, 2011. At: 9:07 AM. This was 6 years ago. Post ID: 2308

Using wild-cards to display various files in a folder with ls.

I recently needed to check whether certain files were in the /usr/lib folder and I used the ls command to do this.

to find all files with the text mad in their file-name.

[email protected] ~ $ ls /usr/lib/*mad*
lrwxrwxrwx. 1 root root  15  18-12-11 11:11 pm /usr/lib/libmad.so.0 -> libmad.so.0.2.1
-rwxr-xr-x. 1 root root 88K  18-12-11 11:11 pm /usr/lib/libmad.so.0.2.1

This command does the same thing as ls /usr/lib/ | grep mad but proves that Linux is very versatile and there are many varied commands to do whatever you want.

[email protected] ~ $ ls /usr/lib/ | grep mad
lrwxrwxrwx.   1 root root    15  20-12-11 08:34 am libmad.so.0 -> libmad.so.0.2.1
-rwxr-xr-x.   1 root root   88K  18-12-11 11:11 pm libmad.so.0.2.1

The bash shell is still the best shell to use, there is zsh,tcsh,csh,sh et cetera but I love using the default bash shell. On something like FreeBSD and NetBSD the default shell is sh, but I do not like using that shell at all. Below is a listing for a useful bash function as seen in an old issue of Atomic magazine. This will weed out all bad symlinks in a folder.

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.

And this command will print out a nice tree listing of your files & folders.

[email protected] Books $ tree -A -s -p -f --dirsfirst
.
├── [drwx------        4096]  ./PDFs
│   ├── [-rw-------     4858305]  ./PDFs/141_lpi_tutorial.pdf
│   ├── [-rw-------     4880536]  ./PDFs/142_lpi_tutorial.pdf
│   ├── [-rw-------     4417401]  ./PDFs/143_lpi_tutorial.pdf
│   ├── [-rw-------     4386087]  ./PDFs/144_lpi_tutorial.pdf
│   ├── [-rw-------     4482385]  ./PDFs/145_lpi_tutorial.pdf
│   ├── [-rw-------     1875853]  ./PDFs/146_lpi_tutorial.pdf
│   └── [-rw-------     1825220]  ./PDFs/147_lpi_tutorial.pdf
├── [-rw-------     4663429]  ./2083+-+A+European+Declaration+of+Independence.docx
├── [-rw-------      888661]  ./50.Things.Youre.Not.Supposed.To.Know.pdf
├── [-rw-------        3089]  ./bigbangtheory.textfile
├── [-rw-------     7664329]  ./[CRC Press] - Unix Administration.pdf
├── [-rw-------      106613]  ./CRONOMICON- BOOK OF SPELLS.PDF
├── [-rw-------     9346436]  ./Linux The Complete Reference.pdf
├── [-rw-------     2232110]  ./Linux The Complete Reference.txt
├── [-rw-------      177924]  ./thedecreesmaster.pdf
├── [-rw-------       11151]  ./thedecreesmaster.txt
├── [-rw-------       11552]  ./writing-skills.pdf
└── [-rw-------      122535]  ./wskposter.ps

1 directory, 18 files

And finally this function that will move all files in the current folder to lower-case. This was also typed in from Atomic magazine.

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
}
1 responses to “Useful Linux/UNIX commands.

Leave a Reply