moDong
moDong

Reputation: 233

How do I make a custom alias to print out a full path with custom text?

I want to make a alias for example if I type fullList it will print out a custom text with specific extension in full path listed the last modified the last something like

>fullList

file = /home/user/something/fileA.txt &
file = /home/user/something/fileB.txt &
file = /home/user/something/fileC.txt & <- the last modified.

Upvotes: 0

Views: 165

Answers (2)

&#201;douard Lopez
&#201;douard Lopez

Reputation: 43391

Function

Use a function instead of an alias. Henceforth, you will be able to pass argument

fullList() {
  customText="$1"

  for f in "$PWD"/* # list current dir files
  do 
      printf "%s: %s\n" "$customText" "$PWD/$f"
  done
}

Usage

Then run

$ fullList 'blabla'
blabla: /path/to/file1
blabla: /path/to/file2
blabla: /path/to/file2

Do you know about ?

The tree command ? Could be helpful to list directories content:

tree -f -L 1 $(pwd)/ 
/home
|-- /home/user1
`-- /home/user2

Upvotes: 1

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11593

If you want your exact example output, you're going to want to do something like.

#!/bin/bash
echo
for i in $(ls -trF *.txt); do 
  full_path="$(pwd $i)/$i"   
  echo "file = $full_path &"
done

And if you want to do a simple one line alias, do something like below.

> alias fullList="echo; for i in \$(ls -trF *.txt); do full_path=\"\$(pwd \$i)/\$i\"; echo \"file = \$full_path &\"; done"
> fullList

file = /some/path/oldest.txt &
file = /some/path/newer.txt &
...
file = /some/path/newest.txt &

Note, this is assuming you only want to find files, since the F flag for ls appends "/" to directories.

Upvotes: 1

Related Questions