Editing
Linux commands
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Text file manipulation == === Display the content of text file === ==== Display the first lines of text file ==== * [http://en.wikipedia.org/wiki/Head_(Unix) head] <nowiki>[options]</nowiki> ''filename'' ({{OS}}: {{Linux}}) * [http://en.wikipedia.org/wiki/Vi vi] <nowiki>[options]</nowiki> ''filename'' (and press {{kbd | key=gg}}) ({{Linux}}) * [http://www.tuxfiles.org/linuxhelp/vimcheat.html vim] <nowiki>[options]</nowiki> ''filename'' (and press {{kbd | key=gg}}) ({{Linux}}); Open file and press {{kbd | key=gg}} for [http://www.vim.org/ gVim] ({{Win}}) ==== Display the first xxx bytes of text file ==== * On {{Linux}}, To display the first bytes characters of text file<ref>[https://unix.stackexchange.com/questions/167814/get-first-x-characters-from-the-cat-command cut - get first X characters from the cat command? - Unix & Linux Stack Exchange]</ref> <pre> head -c 100 file_name </pre> ==== Display the last few lines of text file ==== * [http://en.wikipedia.org/wiki/Tail_(Unix) tail] <nowiki>[options]</nowiki> ''filename'' ({{Linux}}) * [http://unix.stackexchange.com/questions/47407/cat-line-x-to-line-y-on-a-huge-file tail - cat line X to line Y on a huge file - Unix & Linux Stack Exchange] * [http://en.wikipedia.org/wiki/Vi vi] <nowiki>[options]</nowiki> ''filename'' (and press G) ({{Linux}}) * [http://www.tuxfiles.org/linuxhelp/vimcheat.html vim] <nowiki>[options]</nowiki> ''filename'' (and press G) ({{Linux}}); Open file and press G for [http://www.vim.org/ gVim] ({{Win}}) * [http://tailforwin32.sourceforge.net/ Tail for Win32] GUI interface ({{Win}}) {{exclaim}} not support Unicode file for v.4.2.6 ==== Display the entire content of text file ==== * [http://en.wikipedia.org/wiki/Vi vi] <nowiki>[options]</nowiki> ''filename'' ({{Linux}}) * {{kbd | key=<nowiki>cat <filename></nowiki>}} ({{Linux}}) * {{kbd | key=<nowiki>type <filename></nowiki>}} ({{Win}}) ==== Display the partial content of large text file page by page ==== * {{kbd | key=<nowiki>cat <filename> | less</nowiki>}} ({{Linux}} or Cygwin on {{Win}}) {{exclaim}} Chinese issue<ref>[https://stackoverflow.com/questions/6943928/show-special-characters-in-unix-while-using-less-command linux - Show special characters in Unix while using 'less' Command - Stack Overflow] ''un-verified''</ref> * {{kbd | key=<nowiki>type <filename> | more</nowiki>}} ({{Win}}) If the file was encoded in UTF-8, you need to key in {{kbd | key=chcp 65001}} first <ref>[http://fatting10.blogspot.tw/2015/08/solved-window.html (Solved) Window命令視窗中文亂碼 改編碼方法]</ref>. ==== Display only certain lines by number ==== [https://unix.stackexchange.com/questions/288521/with-the-linux-cat-command-how-do-i-show-only-certain-lines-by-number text processing - With the Linux "cat" command, how do I show only certain lines by number - Unix & Linux Stack Exchange] ==== Show certain lines by line number ==== [[Show certain lines by line number]] === Newline count (count the number of lines of a file), word count === * {{kbd | key= <nowiki>wc -l <filename></nowiki>}} for {{Linux}} <ref>[https://en.wikipedia.org/wiki/Wc_(Unix) wc (Unix) - Wikipedia, the free encyclopedia]</ref> ex: {{kbd | key= <nowiki>wc -l *.txt</nowiki>}} to print the newline count of txt files of the current folder * {{kbd | key= <nowiki>find . -type f -exec cat {} + | wc -l</nowiki>}} for {{Linux}} & {{Mac}} to print the newline count of all files on all subdirectories<ref>[https://stackoverflow.com/questions/13727917/use-wc-on-all-subdirectories-to-count-the-sum-of-lines linux - Use wc on all subdirectories to count the sum of lines - Stack Overflow]</ref> * {{kbd | key= <nowiki>wc -l "$(cygpath -u '<path to file>')"</nowiki>}} for cygwin on {{Win}} ex: {{kbd | key= <nowiki>wc -l "$(cygpath -u 'c:\Program Files\file.txt')"</nowiki>}}<ref>[[Cygwin convert windows path to linux path]]</ref> {{exclaim}} [https://stackoverflow.com/questions/28038633/wc-l-is-not-counting-last-of-the-file-if-it-does-not-have-end-of-line-character linux - wc -l is NOT counting last of the file if it does not have end of line character - Stack Overflow] === Save the console message as text file === Save the Unix manpage as plain text file * {{kbd | key= <nowiki>man man | col -b > man.txt</nowiki>}} Thanks! [http://www.electrictoolbox.com/article/linux-unix-bsd/save-manpage-plain-text/ Save a Unix manpage as plain text] === Search text in a file === * grep command for multiple files: {{kbd | key=<nowiki>grep -ir "string to search" /path/to/directory</nowiki>}} or {{kbd | key= grep ''keyword'' /path/to/file}} ({{Linux}}) * vim: (1)[http://www.tuxfiles.org/linuxhelp/vimcheat.html vim] <nowiki>[options]</nowiki> ''filename'' (2)press {{kbd | key= /keyword}} ({{Linux}}) * cat & grep: {{kbd | key= cat /path/to/file <nowiki>|</nowiki> grep ''keyword''}} ({{Linux}}) keyword is case sensitive Search text in a compressed text file * zcat & grep: {{kbd | key= cat /path/to/compressed_text.gz <nowiki>|</nowiki> grep ''keyword''}} ({{Linux}})<ref>[http://www.cyberciti.biz/faq/unix-linux-cat-gz-file-command/ Unix / Linux: cat .GZ Compressed Text File On Screen]</ref> [[Desktop_search#Search_text_in_files| Search text in files]]<ref>[http://linux.vbird.org/linux_basic/0320bash.php#col 鳥哥的 Linux 私房菜 -- 學習 bash shell] </ref> === Merge multiple plain text files === [[Merge multiple text files into one file]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information