I've come across a problem, I am writing a script that would find the biggest file in directory and sub-directories, then show full path on screen. Can't seem to get it working. The error I'm getting is "bash: scriptas.sh: No such file or directory" There are two .mp3 files in the directory.

du -a /dir/ | sort -n -r | head -n 1
echo pwd

I am totally new to this.

  • 2
    That script should work as it is (assuming /dir/ exists). Please provide the complete script, the actual error message (not your interpretation of it) and the output of an ls showing that the dir exists. – Gerald Schneider Dec 19 '18 at 15:10
  • 1
    is scriptas.sh the name of your script? If yes, and it is in your current directory, run it as ./scriptas.sh. And make sure it's executable. – Gerald Schneider Dec 19 '18 at 15:25
  • Could you explain a bit in more in depth about what you just said? Yes, its the name of the script. It's in the directory that I am at the moment. Couldn't understand the last two parts. – FFxY Productions Dec 19 '18 at 15:32
  • The directory where you currently are is by default not in the system path. If you want to execute a script named scriptas.sh that is in your current directory you have to type ./scriptas.sh. And for that it has to be executable (chmod +x scriptas.sh). – Gerald Schneider Dec 19 '18 at 17:08

Like Gerald says in his comment, I think you must set a ./ to execute your script. However, i think your script will return you the folder and not the file.

Personally, i would have used find :

find /dir/ -printf '%s %p\n'| sort -nr | head -1
  • Nice, but you should write head -n 1 instead of head -1, which is obsolete syntax – janos Dec 19 '18 at 17:17
  • yep, you 're right. – blakksnail Dec 19 '18 at 17:18
  • Could you explain to me what -printf '%s %p\n' means/does ? – FFxY Productions Dec 19 '18 at 18:45
  • %s File's size in bytes. %p File's name. – blakksnail Dec 24 '18 at 10:25

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