Useful Linux/UNIX commands.

Posted: December 20, 2017. At: 8:07 PM. This was 2 months ago. Post ID: 2308
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.

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 related to the Perl programming language in the /usr/lib directory.

ubuntu ~ $ ls /usr/lib/*perl*
/usr/lib/libperl.so.5.18  /usr/lib/libperl.so.5.18.2
 
/usr/lib/perl:
5.18  5.18.2
 
/usr/lib/perl5:
Algorithm  auto  Bundle  DBD  DBI  DBI.pm  dbixs_rev.pl  File  HTML  LibAppArmor.pm  Locale  Net  Socket6.pm  Sub  Term  Text  Win32

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

ubuntu ~ $ ls /usr/lib/ | grep perl
libperl.so.5.18
libperl.so.5.18.2
perl
perl5

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.

thx@matrix 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
}

These tips will make your Linux experience better than ever before, especially the scripts that convert files from uppercase to lowercase. This is very useful when bringing files from a Windows PC to a Linux one.

1 responses to “Useful Linux/UNIX commands.

Leave a Reply