Tag Info

Hot answers tagged

21

du -sh is a good place to start. The options are (from man du): -s, --summarize display only a total for each argument -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) To check more than one directory and see the total, use du -sch: -c, --total produce a grand total


19

You should understand that bash is just an execution environment. It executes commands that you call - it's not the business of the shell to even know what the command does, you can call any executable you want. In most cases, it's not even clear what an undo would do - for instance, can you "unplay" a movie? Can you "unsend" an e-mail? What would "undo ...


18

IFS stands for "internal field separator". It is used by the shell to determine how to do word splitting, i. e. how to recognize word boundaries. Try this in a shell like bash (other shells may handle this differently, for example zsh): mystring="foo:bar baz rab" for word in $mystring; do echo "Word: $word" done The default value for IFS consists of ...


15

With tr: echo "Thhiisss iisss mmyyy nameeee" | tr -s 'a-z' Explanation: The -s switch of tr "squeezes" repeat characters. As shown, the switch can be used with a range of characters: a to z.


14

By default, grep uses Basic Regular Expressions, you need to escape the braces to make grep match multiple characters: grep 't\{2\}' textfile Alternatively, you can use the -E option (or -P option for GNU grep, which uses Perl Compatible Regular Expressions) making grep use Extended Regular Expressions, which can use braces without escaping them: grep -E ...


13

No, there is no such thing as an "undo" in Bash, sorry. This is why you should double-check your commands or test them on unimportant files first (if that's possible). And triple-check if you're doing things as root ;)


11

No, there is no generic undo, but some consequences are more reversible than others. Take for example rm: it's essentially used for two purposes - avoiding clutter and freeing space up for new files. With a bit of luck you can retrieve the contents of the file using tools like extundelete, because only the pointer to the start of the file is overwritten when ...


9

I can think of two possible solutions: If you have installed mv from GNU coreutils (which probably is the case), then the following command... find / -name "HAHA" -type f -exec mv --backup=numbered "{}" /home \; ...will move all files called HAHA to /home. The --backup=numbered option of mv ensures that every time the mv command executes, it will check ...


8

Just use the du command: du -sh * will give you the size of all the directories,files etc in current directory in human readable format. You can use the df command to know the free space in the disk: df -h .


7

You need to use the -E (--extended-regexp) or -P (--perl-regexp) option (with GNU grep). According to the GNU grep(1) man page about Basic vs Extended Regular Expressions, i.e. if you choose not to use these options: In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, ...


7

You need to escape the braces: grep 't\{2\}' textfile Otherwise { and } are treated as literal characters.


7

Inside dollared single quotes, some characters are evaluated specially. For example, \n is translated to new line. So, this particular line assigns newline to the variable IFS. IFS, in turn, is a special variable in bash: Internal Field Separator. As man bash says, it is used for word splitting after expansion and to split lines into words with the read ...


7

On a GNU system you'll need to use sed or similar if your locale uses multibyte characters (as jimmij suggests) because GNU tr can only reference a character per byte. In an ASCII locale you can remove all duplicates w/ tr like: LC_ALL=C tr -s '\0-\255' <input So... echo Thhiisss iisss mmyyy nameeee| LC_ALL=C tr -s '\0-\255' ...prints... This is my ...


7

Applications that provide an 'undo' feature do so by maintaining a history of actions taken and either a method of reversing the action or a snapshot of the state of an object to restore a previous state. Commands executed in a shell perform direct actions or execute commands to perform actions. The shell itself would not have the ability to undo the ...


6

du is your friend. If you just want do know the total size of a directory then jump into it and run: du -hs If you also would like to know which sub-folders spend how much disk space?! You could extend this command to: du -h --max-depth=1 | sort -hr which will give you the size of all sub-folders (level 1). The output will be sorted (largest folder on ...


6

As a start, you could print out files with a .jpg file extension with: sudo find / -name *.jpg -print See how that behaves, modify to suit, and you can then pipe the output into another function rather than just printout if you'd like. edit As mentioned in the comments below, this may be a better starting point for you: sudo find / -iname "*.jpg"


6

One way with sed: sed ':X;s/\(.\)\1/\1/g;tX' or even simpler: sed 's/\(.\)\1*/\1/g' (thanks Costas and mikeserv for comments).


5

For short, IFS=$'\n' assign newline \n to variable IFS. $'string' construct is a quoting mechanism which use to decode ANSI C like escape sequences. This syntax comes from ksh93, and was portable to modern shell like bash, zsh, pdksh, busybox sh. This syntax is not defined by POSIX, but was accepted for SUS issue 7.


5

The du command shows the disk usage of the file. The -h option shows results in human-readable form (e.g., 4k, 5M, 3G). du -h (file name)


5

If your shell supports the ksh ${var/search/replace} form of parameter expansion (ksh93, zsh, mksh, yash, bash): for r1 in *R1*; do r2=${r1/R1/R2} singles=${r1/R1/singles} trimmed1=trimmed$r1 trimmed2=trimmed$r2 sickle pe -f "$r1" \ -r "$r2" \ -o "$trimmed1" \ -p "$trimmed2" \ -s "$singles" done POSIXly, you could do ...


5

So here we do have to pass the file name twice in the function. They are not quite the same thing as you notice by observing that one of them is used as the argv[0] value. This doesn't have to be the same as the basename of the executable; many/most things ignore it and you can put whatever you want in there. The first one is the actual path to the ...


4

You have only a couple of ways of doing it: twinkle -c && call sip:in06khattab@sip.linphone.org With this option, the second command is executed when the first one end without error. In other case, the second one never executes. twinkle -c ; call sip:in06khattab@sip.linphone.org In this case, the second command is executed after the first ...


4

I found a simple solution with a small script. The script is called cpf (or whatever name you give it) and is as follows: #!/bin/bash dir=xyz # Directory where you want the files num=1 for file in "$@" do base=`basename -- "$file"` mv -- "$file" "$dir/$base.$num" num=$(($num+1)) done You execute the command as follows: find . -name ...


4

You can get the files with full path with this command find /


4

With -path, you could try: find ~ -path '*/bin/*' -type f This won't list bin itself, so to get both: find ~ \( -path '*/bin/*' -type f \) -o \( -name bin -type d \)


4

You could use find + file and output the name of the files that have mime type image/jpeg: find . -type f -exec sh -c 'file --mime-type "$0" | grep -q image/jpeg && echo "$0"' {} \; or find . -type f -exec sh -c ' mt=$(file --brief --mime-type "$0") [ -z "${mt#image/jpeg}" ] && printf "$0\n" ' {} \;


4

If you are in the company directory then try this: cp file1 sales cp file2 sales OR cp file1 sales; cp file2 sales Or cp file1 file2 file3 sales The last is the easiest and can accomplish copying all files it a single subdirectory in one line. If you want to complete the task and copy each file to each subdirectory in one line, merge the second and ...


3

I would use while loop: i=1 find / -name 'HAHA' -print0 | while read -d '' -r file; do mv "$file" "/home/${file##.*/}$((i++))"; done Important here is print0 option of find which (together with -d '' option of read) treats properly files with white spaces in their names. If you need to do such operation only once then first line which sets i=1 is not ...


3

One way to classify some 'old' commands - though not for 'more powerful' - is that they are builtin commands, meaning a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute. Examples include logout, cd, echo and history. You can see if a ...


3

No, -k1,2 says to sort on the portion of the line that starts at the beginning of the first field and ends at the end of the second field. To sort on the first field and then on the second, it's: sort -k1,1 -k2,2



Only top voted, non community-wiki answers of a minimum length are eligible