How do you -ls another directory's contents from current directory without listing the other directory's full path as command line argument? I also need to sort the contents of the directory by size, and only output the size and name only. For example, I have the following shell script named script.sh
:
#!/bin/bash
ls -lS $1 | awk '{print $5, $9}'
The -lS lists files by size, and the awk prints only the size and file name as output. The $1 is the command line argument, but right now, it can only take in a directory's full path for this to work. For example, I have a directory whose path is /accounts/documents/folder/directory1
.
To call this script, I have to type $ script.sh /accounts/documents/folder/directory1
for this to work. Is there something I can change in my script so that in command line, I only have to type $ script.sh directory1
for this to work?
Any help is appreciated!
Assuming the directory you are looking for is always a subdirectory, the following script should work :
#!/bin/bash
for FILE in `find . -name "$1" `
do
if [ -d $FILE ]
then
ls -lS $FILE | awk '{print $5, $9}'
fi
done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.