9

How do I truncate output in BASH?

For example, if I "du file.name" how do I just get the numeric value and nothing more?

later addition:
all solutions work perfectly. I chose to accept the most enlightning "cut" answer because I prefer the simplest approach in bash files others are supposed to be able to read.

4 Answers 4

14

If you know what the delimiters are then cut is your friend

du | cut -f1

Cut defaults to tab delimiters so in this case you are selecting the first field.

You can change delimiters: cut -d ' ' would use a space as a delimiter. (from Tomalak)

You can also select individual character positions or ranges:

ls | cut -c1-2
Sign up to request clarification or add additional context in comments.

1 Comment

You can add something like "cut -d ' ' -f 1-2", to point out how to change the characters cut uses as delimiters.
10

I'd recommend cut, as others have said. But another alternative that is sometimes useful because it allows any whitespace as separators, is to use awk:

du file.name | awk '{print $1}'

1 Comment

+1 If the delimiter could be any whitespace, then awk is the most robust solution (even if it's not as pretty as using cut).
6
du | cut -f 1

7 Comments

beat me to it by 20 seconds :)
Beat me by 30 seconds :) I've deleted my duplicate answer and added an alternative answer instead.
I'd prefer more information about delimiters and ranges since he was only using du as an example. Then I'll happily delete too :)
Can I just copy yours or do I have to invent my own? :-)
Please just copy if you think they are ok - if not invent your own
|
1

If you just want the number of bytes of a single file, use the -s operator.

SIZE=-s file.name

That gives you a different number than du, but I'm not sure how exactly you're using this.

This has the advantage of not having to run du, and having bash get the size of the file directly.

It's hard to answer questions like this in a vacuum, because we don't know how you're going to use the data. Knowing that might suggest an entirely different answer.

4 Comments

In the original question he said that using du was just an example; your answer doesn't help with his question (although it may in fact be useful to him if his example wasn't just hypothetical)
Yes, I read that. And I didn't interpret it as "du was just an example."
I don't understand how this is used. What's the context?
Please explain "If you just want the number of bytes of a single file, use the -s operator." If I type that at a Bash prompt, I get a "file.name: command not found" error (for a file that exists with that name). How do I make the -s operator work? The only one I know of is for the conditional expression to test whether a file exists and is not empty: [[ -s file.name ]] && echo "yes" for example, but that doesn't tell me the actual size.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.