0

I have restricted shell on Production environment servers in my company, leaving me with handful of command choices.

I am trying to get full path of files/directories using only ls command. I can't use xargs, sed, awk, find in the restricted shell, so I am only left with using options in the ls command.

I have tried below command but this doesn't recurse. I mention this as I came across this command while searching an answer for my problem.

ls -lRt -d -1 $PWD/{*,.*}

Here is the list of ALL the commands available to me in restricted shell. Let me be clear. I can't create any function, I can't use read command.

/usr/bin/cat
/usr/bin/clear
/usr/bin/cmp
/usr/bin/cut
/usr/bin/date
/usr/bin/df
/usr/bin/diff
/usr/bin/du
/usr/bin/grep
/usr/bin/head
/usr/bin/id
/usr/bin/ls
/usr/bin/od
/usr/bin/prstat
/usr/bin/ps
/usr/bin/sort
/usr/bin/tail
/usr/bin/wc
/usr/bin/which
/usr/bin/who
cd
echo
exit
pwd
  • What shell is it? ksh, bash, zsh, dash? – xenoid Oct 22 '18 at 16:01
  • echo $SHELL /usr/SYSADM/bin/rbash – user3112966 Oct 22 '18 at 19:26
0

The following function will list all files from a directory (/ by default) in depth-first order without using even ls:

lsr(){
        for f in "$1/"* "$1/".[!.]* "$1/"..?*; do
                test -e "$f" && echo "$f"
                test -d "$f" && lsr "$f"
        done
}

Example:

$ lsr .
./dir
./dir/file
./dir/.dotfile
./.foo
./..bar
$ find . -depth
./dir/.dotfile
./dir/file
./dir
./.foo
./..bar
.

It can be made into a separate script instead of a function:

#! /bin/sh
for f in "$1/"* "$1/".[!.]* "$1/"..?*; do
        test -e "$f" && echo "$f"
        test -d "$f" && "$0" "$f"
done
  • Thanks mosvy. But as I mentioned in my question, I have restricted shell with approximately 20 Unix commands allowed (e.g., cat, ls, df, du, tail, echo..) . So, I can't create a function or call another shell script. Unfortunately, I am only limited to using ls command and it's options. It is possible, that I may not have any way to fulfill my requirement with the extremely limited options that I have. – user3112966 Oct 20 '18 at 23:35
  • or g=*; while ls -1d $g 2>/dev/null; do g="$g/*"; done – mosvy Oct 21 '18 at 4:23
  • or if you have du: for f in `du`; do ls -ld "$f"/* "$f"/.* 2>/dev/null; done – mosvy Oct 21 '18 at 4:49
  • 2
    Please edit your question with the list of commands you have access to and mention the fact that you can't define functions (you should've done that from the beginning!) – mosvy Oct 21 '18 at 4:56
0

This command should do it as echo is available.

ls -Rr1 . |  while read line; do case $line in *:) basepath=${line%:};; "") basepath=;; *) echo "$basepath/$line";; esac; done
0

Can't tell what subset of bash is implemented in rbash. If you are lucky, try

shopt -s globstar ## bash built in 
ls **/* *

(* lists files in the current directory, and **/* lists all files in all subdirectories, at whatever depth).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.