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!
== File operation == === Find a file by filename === * {{Gd}} {{kbd | key=locate}} command ({{Linux}}) ex: {{kbd | key= locate ''filename''}} to locate the filename quickly. <ref>[https://blog.longwin.com.tw/2008/01/linux_quick_find_file_locate_2008/ Linux 快速尋找檔案 - locate - Tsung's Blog]</ref> * {{kbd | key=find}} command ({{Linux}}) ex: {{kbd | key= sudo find / -iname ''filename''}} Find the '''case-insensitive''' file name Under the path / (root folder) <ref> [http://unix.stackexchange.com/questions/32155/find-command-how-to-ignore-case Find command: how to ignore case? - Unix and Linux]; Find the '''case-sensitive''' file name Under the path / (root folder) ex: {{kbd | key= find / -name ''filename''}} ref:[http://www.codecoffee.com/tipsforlinux/articles/21.html Tips For Linux - How to find files in Linux using 'find']</ref> * {{kbd | key=dir}} command ({{Win}}) ex: {{kbd | key= dir /s ''filename''}} <ref>[http://www.computerhope.com/issues/ch000309.htm How to find a file in MS-DOS.]</ref><ref>/S 顯示指定目錄及所有子目錄中的檔案。 (引用自 dir /? 命令說明)</ref> {{exclaim}} I have no idea to specify the file path to find the file. === Copy & overwrite file === Copy and overwrite file without prompt (confirmation) * On {{Linux}} {{kbd | key=cp -f ''to_be_copy.file'' ''to_be_overwrote.file''}} <ref>[http://www.ewdna.com/2012/05/linux-cp-force-overwrite.html Linux:檔案複製強制覆寫, cp force overwrite @ 符碼記憶]</ref> * On {{Linux}} {{kbd | key=/bin/cp -f ''to_be_copy.file'' ''to_be_overwrote.file''}} * On {{Win}} {{kbd | key=<nowiki>copy /Y C:\source\some.file C:\target\</nowiki>}}<ref>[https://home.csulb.edu/~murdock/copy.html DOS Command: COPY]</ref> ==== Copy old directory to new directory ==== * {{kbd | key=cp -a ''old_dir new_dir/''}} or {{kbd | key=cp -a ''old_dir/ new_dir/''}} ({{Linux}})<ref> <pre>from manual: -a, --archive same as -dR --preserve=all -d same as --no-dereference --preserve=links --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible addi- tional attributes: context, links, xattr, all -R, -r, --recursive copy directories recursively</pre></ref><ref>[https://superuser.com/questions/61611/how-to-copy-with-cp-to-include-hidden-files-and-hidden-directories-and-their-con unix - How to copy with cp to include hidden files and hidden directories and their contents? - Super User]</ref> ** If the folder {{kbd | key=new_dir}} exists already, system will create the folder {{kbd | key=old_dir}} and all files and sub folders under {{kbd | key=old_dir}} will copy to the path {{kbd | key=new_dir}}/{{kbd | key=old_dir}}. ** If the folder {{kbd | key=new_dir}} NOT exists, all files and sub folders under {{kbd | key=old_dir}} will copy to the folder {{kbd | key=new_dir}}. * {{kbd | key=cp -a ''old_dir/* new_dir/''}} ({{Linux}}): All files and sub folders under {{kbd | key=old_dir}} will copy to the folder {{kbd | key=new_dir}}. * {{kbd | key=copy /Y ''old_dir new_dir/''}} ({{Win}})<ref> /Y 不顯示覆寫現存目的檔案的確認提示。 (引用自 copy /? 命令說明)</ref> * make new directory & copy all old files to new directory ** mkdir ''new_dir'' ** cp ''old_dir/* new_dir'' * [https://askubuntu.com/questions/91740/how-to-move-all-files-in-current-folder-to-subfolder How to move all files in current folder to subfolder? - Ask Ubuntu] ==== Rename (move) files or directory ==== * {{kbd | key=mv ''/path/to/old_folder_name /path/to/new_folder_name''}} ({{Linux}})<ref>[http://www.liamdelahunty.com/tips/linux_rename_directory.php Linux Rename Files, Folders or Directories]</ref> * {{kbd | key=mv ''/path/to/old.file /path/to/new.file''}} ({{Linux}}) * {{kbd | key=mv ''/path/to/file /path/to/new_folder_name''}} ({{Linux}}) * {{kbd | key=rename ''old_folder_name new_folder_name''}} ({{Win}})<ref>[http://www.computerhope.com/renamehl.htm MS-DOS ren and rename command help]</ref> === Delete files or directory === delete all files or directory * {{kbd | key=rm -rf ''/Name_of_directory''}} to delete all files under the '''Name_of_directory'' ({{Linux}})<ref><nowiki>[OPTION]</nowiki> * -r remove directories and their contents recursively; * -f ignore nonexistent files, never prompt (above content cited from linux documentation: rm --help) </ref> * {{kbd | key=del ''Name_of_directory''}} ({{Win}}) ** After executed ''del Name_of_directory'' but the empty directory directory_name will be left. ** Command delete is not available in Win XP * rsync ** {{Gd}} quick for many small files (1) create a empty folder ex: {{kbd | key=<nowiki>mkdit /path/to/source</nowiki>}} (2) To empty the target folder, keyin {{kbd | key=<nowiki>rsync -avP --delete /path/to/source/ /path/to/target/</nowiki>}} ({{Linux}}) ** {{kbd | key=<nowiki>rsync --delete-before -a -H -v --progress --stats /path/to/empty/folder/ /path/to/target_folder_will_be_deleted/</nowiki>}} ({{Linux}})<ref>[http://blog.sina.com.cn/s/blog_5f5a3dff0101e2tm.html Linux下使用rsync最快速删除海量文件的方法_果冻的剥壳_新浪博客]</ref> ''un-verified'' * Using SFTP/FTP to connection to the server and delete selected files. {{exclaim}} It may costs too much time if there are many files or sub folders! Delete the files which their file name were matched with the ''naming rule'' when the number of files is too many. * {{kbd | key= <nowiki>find . -name 'naming_rule' -delete</nowiki>}} or {{kbd | key= <nowiki>find . -name 'naming_rule' -type f -delete</nowiki>}} <ref>[http://danilo.ariadoss.com/how-to-recursively-remove-ds_store-files-on-mac-os-x/ How to recursively remove .DS_Store files on Mac OS X | Tangential Musings]</ref><ref>[http://xyz.cinc.biz/2015/12/linux-rm-argument-list-too-long.html XYZ的筆記本: Linux 使用 rm 刪除檔案,出現 /bin/rm: Argument list too long]</ref><ref>[http://www.hypexr.org/linux_find_help.php Linux find command]</ref> === Download file from remote server === * FTP * wget (with progress bar): {{kbd | key= <nowiki>wget http://path.to/file</nowiki>}} * SVN update: {{kbd | key= <nowiki>svn up http://remote.svn.server/path/ /local/path/ --username=your_account</nowiki>}} and key in your SVN password <ref>using the www account {{kbd | key= <nowiki>su - www -c "svn up http://remote.svn.server/path/ /local/path/ --username=your_account --password=xxx" </nowiki>}}</ref> * more on [[File transfer methods]] === File compression and decompression === compress * {{kbd | key= <nowiki>pv big.file | gzip > big.file.gz</nowiki> }}({{Linux}})<ref>[http://www.catonmat.net/blog/unix-utilities-pipe-viewer/ A Unix Utility You Should Know About: Pipe Viewer - good coders code, great reuse]</ref> keep the original file * {{kbd | key= gzip -v big.file}}({{Linux}}) NOT keep the original file. File name after compressed: big.file.gz * [http://www.cyberciti.biz/faq/how-do-i-compress-a-whole-linux-or-unix-directory/ How do I Compress a Whole Linux or UNIX Directory?] for {{Linux}} decompress * {{kbd | key= gunzip -c big.file.gz > big.file}}({{Linux}}) keep the gz file<ref>[http://superuser.com/questions/45650/how-do-you-gunzip-a-file-and-keep-the-gz-file linux - How do you gunzip a file and keep the .gz file? - Super User]</ref> * {{kbd | key= gzip -d big.file.gz}}({{Linux}}) NOT keep the gz file ref * [http://www.cyberciti.biz/faq/tar-extract-linux/ Linux: tar Extract Files] === List directory or files / list directory contents === * ls ({{Linux}}) [http://linuxcommand.org/man_pages/ls1.html ls - list directory contents] ** List detailed information of files. e.g. {{kbd | key= ls -lh}}<ref>[http://linux.vbird.org/linux_basic/0210filepermission.php 鳥哥的 Linux 私房菜 -- Linux 的檔案權限與目錄配置]</ref> <ref>[OPTION] * -l use a long listing format * -h, --human-readable; with -l, print sizes in human readable format (e.g., 1K 234M 2G) Quoted from linux {{kbd | key=man ls}}</ref> ** List detailed information of files and sort by modification time. {{kbd | key= ls -lht}} ** List top N files and sort by modification time. {{kbd | key= <nowiki>ls -lht | head -N</nowiki>}} e.g. {{kbd | key= <nowiki>ls -lht | head -10</nowiki>}} First line is the total file size of specified folder.<ref>[http://www.unix.com/unix-for-dummies-questions-and-answers/44493-how-list-top-10-files.html How to list top 10 files | Unix Linux Forums | UNIX for Dummies Questions & Answers]</ref> * ll ({{Linux}}) {{kbd | key= ll}} = {{kbd | key= ls -l}} * Print the entire path of files and '''level-unlimited''' sub-directories in the current directory. On {{Win}} using {{kbd | key= dir/s/b}}, and on {{Linux}} using {{kbd | key= find /path/to/files/ -print}} <ref>[http://superuser.com/questions/159975/making-ls-output-like-dir-b-s linux - Making ls output like dir /b /s - Super User]</ref>. * {{Gd}} create ls command in {{Win}} command prompt: {{kbd | key = echo dir %1 > %systemroot%\system32\ls.bat}} Thanks, hmjd!<ref>[http://stackoverflow.com/questions/9362692/how-to-create-ls-in-windows-command-prompt How to create ls in windows command prompt? - Stack Overflow]</ref> * List all files and directories including hidden ones in the current directory. On {{Win}} is {{kbd | key=<nowiki>dir /a</nowiki>}}, and on {{Mac}} or {{Linux}} is {{kbd | key=<nowiki>ls -a</nowiki>}}<ref>[http://www.computerhope.com/issues/ch001039.htm How to see hidden files in MS-DOS and the Command Prompt]</ref><ref>[http://www.cyberciti.biz/faq/bash-shell-display-only-hidden-dot-files/ Bash Shell: Display All Hidden Dot Files In a Directory]</ref>. List only filenames and one file per line * {{Win}} {{kbd | key=dir/b}}<ref>[https://smallbusiness.chron.com/copy-list-files-windows-folder-excel-list-40032.html How to Copy a List of Files in a Windows Folder Into an Excel List]</ref> * {{Mac}} {{kbd | key=ls -1}}<ref>[https://www.baeldung.com/linux/list-one-filename-per-line List One Filename Per Line in Linux | Baeldung on Linux]</ref> Verify if a file or a directory exists * On {{Linux}}: {{kbd | key= ls /path/to/file}} or {{kbd | key= ls -d /path/to/directory/}} e.g. {{kbd | key= ls -d ~/}} to list your home directory<ref>[https://linuxconfig.org/single-linux-command-to-return-to-home-directory Single linux command to return to home directory]</ref>. * On {{Win}}: ** {{kbd | key= dir C:\path\to\file}} or {{kbd | key= dir C:\path\to\directory\}} ** {{kbd | key= IF EXIST}} <ref>[http://stackoverflow.com/questions/3022176/how-to-verify-if-a-file-exists-in-a-windows-bat-file How to verify if a file exists in a Windows .BAT file? - Stack Overflow]</ref><ref>[http://stackoverflow.com/questions/2903416/windows-shell-string-operations-changing-backslash-to-slash Windows shell string operations (changing backslash to slash) - Stack Overflow]</ref> <pre> SET a=C:\path\to\file REM -- If the directory was separated the \ symbol & the path NOT contains space IF EXIST %a% (ECHO YES) ELSE (ECHO No) SET a="C:\path\to\directory contains space\space" REM -- If the directory was separated the \ symbol & the path contains space IF EXIST %a% (ECHO YES) ELSE (ECHO No) SET a=C:/path/to/file REM -- If the directory was separated the / symbol, the next command replace / with \ symbol. SET a=%a:/=\% IF EXIST %a% (ECHO YES) ELSE (ECHO No) </pre> troubleshooting <pre> SET a = C:\path\to\file REM -- Correct command: SET a=C:\path\to\file IF EXIST %a% (ECHO YES) ELSE (ECHO No) REM -- the returned result will not correct! </pre> === Print the current directory / where am i === * {{Linux}}: {{kbd | key= pwd}} '''p'''rint name of '''w'''orking '''d'''irectory. More on [http://linuxcommand.org/man_pages/pwd1.html pwd]. * {{Win}}: {{kbd | key= cd}} or {{kbd | key= echo %cd%}} to print '''c'''urrent '''d'''irectory <ref>[http://stackoverflow.com/questions/921741/windows-equivilent-to-unix-pwd Windows equivalent to UNIX pwd - Stack Overflow]</ref> === Change directory to my home directory === * {{kbd | key= cd ~/}} or {{kbd | key= cd $HOME}} ({{Linux}}) will switch to my home directory ex: /Users/ACCOUNT<ref>[http://superuser.com/questions/271925/where-is-the-home-environment-variable-set linux - Where is the $HOME environment variable set? - Super User]</ref> * {{kbd | key= cd %UserProfile%}} ({{Win}}) will switch to my home directory ex: C:\Users\ACCOUNT more on [[MS_Windows_Explorer]]<ref>[http://weka.wikispaces.com/Where+is+my+home+directory+located%3F weka - Where is my home directory located?]</ref> === How to list the ten most recent files in the same directory === [[How to list the ten most recent files in the same directory]]
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