0

I have got a task where I have a js file where I shall use nodejs and the exec() command to list all files in the current directory I am in. I do not know the name of the current directory. How do I write the exec() and ls command in the js file to be able to get a list of the files?

Can anyone in an east way tell me of to write that piece of code?

1

1 Answer 1

3

That should be fine. The module 'util' is to promisify exec(), for it to return a promise, then you await it, and can use it.

    const util = require('util')
    const exec = util.promisify(require('child_process').exec)
    
    const main = async() => {
            const { stdout, stderr } = await exec('ls', ['a', '-l' ])
            if (stderr) {
                    console.log(stderr)
            }   
            console.log(`the list of files in this directory is: ${stdout}`)
    }
    main()

exec (spawn and fork as well) are fork process (child process) from the main node process (mother process), means that they run on a concurrent process, detached from the mother one. It's very efficient as they use another thread of the processor.

In the case of exec, it open a new terminal (tty) and run on it, so the command exec('ls', ['a','-l'}) is running 'ls -l' cmd in a shell (i don t know why the 'a'). Because this exec child-process is running into a shell, any shell command can be executed: I could have done await exec('find . -type f | wc -l') return the number of files, or await exec('cat fileName.js') (print out the content of fileName in the terminal) return the content of fileName.js into stdout.

Exec return a call-back with (err, stdout, stderr), for this snippet to be more clear, here util.promisify() promisify the call-back and then we can use async/await or .then().catch(). We also could have done:

const util = require('util')
const { exec } = require('child_process')        
const ex = util.promisify(exec)
const main = async() => {
                const { stdout, stderr } = await ex('ls')
                if (stderr)...

In fact more basically this code could be written like this

const { exec } = require('child_process');
exec('ls', (error, stdout, stderr) => {
  if (error) {
          console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.error(`stderr: ${stderr}`);
});

See the nodejs doc https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

hope it helps

3
  • Thank you so much for you answer! Would you be able to elaborate a bit how this code works :D I am completely new to this. Thanks a lot!
    – Gabriella
    Dec 10, 2020 at 13:08
  • My pleasure, see the edit of the previous post.
    – Jerome
    Dec 11, 2020 at 6:55
  • Thanks a lot @Jerome! That helped a lot and the more basically code where easier for me to understand. :D
    – Gabriella
    Dec 11, 2020 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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