

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Listing modified, old and newly created files on Linux using C++
<p>Here we will see how to list the modified files and old and newly created files on Linux platform using C++ program.</p><p>The task is very simple. We can use the Linux shell command to get the files in desired order. The ls –l command is used to get all of the files in long listing format. Here we will add more options to sort them based on time. (Ascending and Descending). The –t command is used to sort based on time, and –r can be added to reverse the sequence.</p><p>The command will be like below:</p><pre class="result notranslate">ls –lt ls –ltr</pre><p>We will use these commands using the system() function in C++, to get the result from C++ code.</p><h2>Example Code</h2><pre class="prettyprint notranslate">#include<iostream> using namespace std; main(){ //Show the files stored in current directory descending order of their modification time cout << "Files List (First one is newest)" << endl; system("ls -lt"); //use linux command to show the file list, sorted on time cout << " Files List (First one is oldest)" << endl; system("ls -ltr"); //use the previous command -r is used for reverse order }</pre><h2>Output</h2><pre class="result notranslate">Files List (First one is newest) total 32 -rwxr-xr-x 1 soumyadeep soumyadeep 8984 May 11 15:19 a.out -rw-r--r-- 1 soumyadeep soumyadeep 424 May 11 15:19 linux_mod_list.cpp -rw-r--r-- 1 soumyadeep soumyadeep 1481 May 4 17:03 test.cpp -rw-r--r-- 1 soumyadeep soumyadeep 710 May 4 16:51 caught_interrupt.cpp -rw-r--r-- 1 soumyadeep soumyadeep 557 May 4 16:34 trim.cpp -rw-r--r-- 1 soumyadeep soumyadeep 1204 May 4 16:24 1325.test.cpp Files List (First one is oldest) total 32 -rw-r--r-- 1 soumyadeep soumyadeep 1204 May 4 16:24 1325.test.cpp -rw-r--r-- 1 soumyadeep soumyadeep 557 May 4 16:34 trim.cpp -rw-r--r-- 1 soumyadeep soumyadeep 710 May 4 16:51 caught_interrupt.cpp -rw-r--r-- 1 soumyadeep soumyadeep 1481 May 4 17:03 test.cpp -rw-r--r-- 1 soumyadeep soumyadeep 424 May 11 15:19 linux_mod_list.cpp -rwxr-xr-x 1 soumyadeep soumyadeep 8984 May 11 15:19 a.out</pre>
- Related Questions & Answers
- Listing out directories and files using C#
- Learn how to find and list down recently modified files in linux
- Listing out directories and files in Python?
- Create and display the newly created database in MongoDB?
- The best way to compress and extract files using the tar command on linux
- Find the Largest Top 10 Files and Directories On a Linux
- MongoDB - How to copy rows into a newly created collection?
- How to find and sort files based on modification date and time in linux
- How to search contents of multiple pdf files on Linux?
- How to Recursively Search all Files for Strings on a Linux
- How to download APK files from Google Play Store on Linux
- Understanding .a , .so and .la library files in Linux
- How to find all files with names containing a string on Linux?
- How to remove files and directories in the Linux operating system using the terminal?
- How to Protect Files and Directories from Deleting in Linux
Advertisements