Create a new command in bash...

Asked by khaled ney on 2012-12-13

i want to create a command that lists the names of all the files in another directory and their size in bytes.
help pls

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu bash Edit question
Assignee:
No assignee Edit question
Last query:
2012-12-13
Last reply:
2012-12-18
arochester (arochester) said : #1

What have YOU done so far?

N1ck 7h0m4d4k15 (nicktux) said : #2

This command already exists , and is du

Open a terminal and write

man du

for more info .

To point at a specific directory and list all the files from the directory with the size in bytes , We assume the Downloads directory , give in terminal

du -ab Downloads/

Thanks

You don't need a new command, you just need the du command.

 du --max-depth=1 -hc $HOME

For example.

khaled ney (k-awad) said : #4

is there any other command do the same but searches for files whose name matches a pattern you supply

Warren Hill (warren-hill) said : #5

You can combine du with grep for example

 du -d1 -hc $HOME | grep foo

Khaled,

I suggest reading these guides:

https://help.ubuntu.com/community/ExternalGuides

Midnight Matt (randygaffer) said : #7

Try this at the prompt:
$ ls -a [directory] | while read x; do if [[ -f [directory]/$x ]]; then echo "$x: `wc -c < [directory]/$x` bytes"; fi; done
*Replace `[directory]` with the directory pathname.*

Or create an executable file w\ this content:

#!/bin/bash

ls -a "$1" | while read x; do
 if [[ -f "$1/$x" ]]; then
  echo "$x: `wc -c < "$1/$x"` bytes"
 fi
done

exit 0

Or create a new function in your .bashrc or .bash_functions file:

function_name()
 {
 ls -a "$1" | while read x; do
  if [[ -f "$1/$x" ]]; then
   echo "$x: `wc -c < "$1/$x"` bytes"
  fi
 done

 return 0
 }
*Replace `function_name` w\ any command name you fancy. In any executable file or function, `$1` is the system variable representing the 1st argument passed. `$2` represents the 2nd argument passed, etc.*

Good luck.

Can you help with this problem?

Provide an answer of your own, or ask khaled ney for more information if necessary.

To post a message you must log in.